我想用方括号替换主题标签,并在第一个方括号后添加一个单词,并且所有字符串都需要小写。
示例字符串:
$str = "This is some text
<p>##IDOBJECT=784##</p> and another some text
<p>##IDOBJECT=1509##</p>
<p>##LATESTARTICLESHOME=321##</p>
<p align=\"center\">##IDOBJECT=321##</p>";
Run Code Online (Sandbox Code Playgroud)
我想用 替换##IDOBJECT=123##格式化字符串[object idobject=123]。请注意,这里在第一个括号和字符串转换object为 后添加了额外的单词。我尝试使用此 正则表达式来查找这些字符串,但无法按照我的描述进行替换。[IDOBJECTidobject/(\##.*?\##)/
您的正则表达式是正确的,您只需调整捕获的位置即可。将组移动到#s 内。也不#是特别的,所以不需要转义。
##(.*?)##
Run Code Online (Sandbox Code Playgroud)
演示: https: //regex101.com/r/meYYna/1/
...或者我可能读错了,如果你想小写返回以及使用preg_replace_callbackwith strtolower。
$str = "This is some text
<p>##IDOBJECT=784##</p> and another some text
<p>##IDOBJECT=1509##</p>
<p>##LATESTARTICLESHOME=321##</p>
<p align=\"center\">##IDOBJECT=321##</p>";
echo preg_replace_callback('/##(.*?)##/', function($match){
return strtolower('[object ' . $match[1] . ']');
}, $str);
Run Code Online (Sandbox Code Playgroud)