输入字符串:
<ref> Hi this one to be rmoved} {{sdjfhk arbit shit}} [whatever] </ref> But this one to be preserved. <ref> again this has to be removed. </ref>
输出字符串:
But this one to be preserved.
我试过:$str = preg_replace('/^<ref.*?ref>$/', '', $str);
答案 0 :(得分:0)
你非常接近。您只想删除^
和$
个字符。
$str = '<ref> Hi this one to be rmoved} {{sdjfhk arbit shit}} [whatever] </ref> But this one to be preserved. <ref> again this has to be removed. </ref>';
print preg_replace('/<ref.*?ref>/', '', $str);
//=> But this one to be preserved.
See it working here on tehplayground.com
旁注:由于您正在寻找HTML标记,因此技术上应该使用DOM解析器。
PHP Simple HTML DOM Parser非常适合这种任务:)