开发者

regex remove ref tags php

开发者 https://www.devze.com 2023-03-29 01:59 出处:网络
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>

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

0

精彩评论

暂无评论...
验证码 换一张
取 消