我想替换我编码的字符串
jsbdahsbdaisd [[aaa]]ID1#random text 1[[/aaa]] asdasdasd [[aaa]]ID2#random text 2[[/aaa]]
类似
jsbdahsbdaisd <span onclick="function(ID1)">random text 1</span> asdasdasd <span onclick="function(ID2)">random text 2</span>
是否可以在一行中使用preg_replace执行此操作?
由于
答案 0 :(得分:0)
我做了这个作为查找/替换:
<?php
$string = 'jsbdahsbdaisd [[aaa]]ID1#random text 1[[/aaa]] asdasdasd [[aaa]]ID2#random text 2[[/aaa]]';
$new_string = preg_replace('/\[\[aaa\]\](ID[0-9]+)#([-A-Z0-9 _.]+)\[\[\/aaa\]\]/i', '<span onclick="function($1)">$2</span>', $string);
print $new_string;
输出:
jsbdahsbdaisd <span onclick="function(ID1)">random text 1</span> asdasdasd <span onclick="function(ID2)">random text 2</span>