如何在PHP中删除自定义标记和内容?

时间:2022-11-26 07:39:05

Say if I had this tag in a string:
[phpbay]{keyword}, 30, "", "", "", "", "", "", "", "", "", "", "", "", "2", "", "", ""[/phpbay]
Could I remove it and the content between the tags? Maybe using regex? Thanks!

假如我在字符串中有这个标签:[phpbay] {keyword},30,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“ “,”“,”2“,”“,”“,”“[/ phpbay]我可以删除它和标签之间的内容吗?也许使用正则表达式?谢谢!

5 个解决方案

#1


1  

preg_replace('~\[phpbay\](.+?)\[/phpbay\]~','',$string);

#2


1  

$string = "[phpbay]blah blah blah [/phpbay]";
$stripped_string = preg_replace('~\[\/?phpbay\]~', '', $string);

if I'm reading your question right, and want only the "blah blah blah" part to remain.

如果我正确地阅读你的问题,并希望只留下“等等等等”的部分。

#3


1  

This regEx will give you everything between those two tags:

这个regEx将为您提供这两个标签之间的所有内容:

\[phpbay](.+)\[/phpbay]

#4


1  

Yes, a regex might be feasible here:

是的,这里的正则表达式可能是可行的:

$str = preg_replace('#\[phpbay][{\w},\s\d"]+\[/phpbay]#', "", $str);

This just removes the "tag" from the string if it contains only such characters as in your example. If you only wanted to remove it if it contains e.g. your example 30 or a specific {keyword} you would have to make the regex more specific.

这只是从字符串中删除“标记”,如果它只包含示例中的字符。如果你只想删除它,如果它包含例如您的示例30或特定的{keyword}您必须使正则表达式更具体。

See also https://*.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world for some tools that might help.

有关可能有用的一些工具,另请参阅https://*.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world。

#5


-1  

I personally would use str_replace or str_ireplace (case Insensitive)

我个人会使用str_replace或str_ireplace(case Insensitive)

$Find = array("[phpbay]","[/phpbay]");
$Replace = array("<b>","</b>");
$Source = "[phpbay]Hello World[/phpbay]";


//Couldn't get any simpler...Replace [phpbay]Content[/phpbay] with <b>Content</b>

echo str_ireplace($Find,$Replace,$Source);

Tried and tested, it works.

经过试验和测试,它确实有效。

Hope this helps!

希望这可以帮助!

#1


1  

preg_replace('~\[phpbay\](.+?)\[/phpbay\]~','',$string);

#2


1  

$string = "[phpbay]blah blah blah [/phpbay]";
$stripped_string = preg_replace('~\[\/?phpbay\]~', '', $string);

if I'm reading your question right, and want only the "blah blah blah" part to remain.

如果我正确地阅读你的问题,并希望只留下“等等等等”的部分。

#3


1  

This regEx will give you everything between those two tags:

这个regEx将为您提供这两个标签之间的所有内容:

\[phpbay](.+)\[/phpbay]

#4


1  

Yes, a regex might be feasible here:

是的,这里的正则表达式可能是可行的:

$str = preg_replace('#\[phpbay][{\w},\s\d"]+\[/phpbay]#', "", $str);

This just removes the "tag" from the string if it contains only such characters as in your example. If you only wanted to remove it if it contains e.g. your example 30 or a specific {keyword} you would have to make the regex more specific.

这只是从字符串中删除“标记”,如果它只包含示例中的字符。如果你只想删除它,如果它包含例如您的示例30或特定的{keyword}您必须使正则表达式更具体。

See also https://*.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world for some tools that might help.

有关可能有用的一些工具,另请参阅https://*.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world。

#5


-1  

I personally would use str_replace or str_ireplace (case Insensitive)

我个人会使用str_replace或str_ireplace(case Insensitive)

$Find = array("[phpbay]","[/phpbay]");
$Replace = array("<b>","</b>");
$Source = "[phpbay]Hello World[/phpbay]";


//Couldn't get any simpler...Replace [phpbay]Content[/phpbay] with <b>Content</b>

echo str_ireplace($Find,$Replace,$Source);

Tried and tested, it works.

经过试验和测试,它确实有效。

Hope this helps!

希望这可以帮助!