正则表达式删除ref标签php

时间:2011-08-18 05:37:25

标签: php regex

输入字符串:

<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);

1 个答案:

答案 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非常适合这种任务:)