Input string:
<ref> Hi this one to be rmoved} {{sdjfhk arbit shit}} [whatever] </ref> But this one to be preserve开发者_运维百科d. <ref> again this has to be removed. </ref>
Output String:
But this one to be preserved.
i tried: $str = preg_replace('/^<ref.*?ref>$/', '', $str);
You were very close. You just want to drop your ^
and $
characters.
$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
Side note: Since you're looking for HTML tags, you should technically be using a DOM parser for this.
PHP Simple HTML DOM Parser is great for this kind fo task :)
精彩评论