PHP Regex to remove non-related content but leave content within blocks
I need help to fix my regex, say if there is text like:
bl开发者_StackOverflow中文版a bla [bla1]something[/bla1] bla bla [bla2]something else[/bla2] bla bla bla
I want everything in bla1 and bla2 removed, including those tags. Can someone help me?
Thank you.
If all you need is just to remove tags, then try:
$str = 'bla bla [bla1]something[/bla1] bla bla [bla2]something else[/bla2] bla bla bla';
$result = preg_replace('~\[([^\]]+)\].*?\[/\\1\]~', '', $str);
var_dump($result);
Use regex:
\[bla1\]\s*(((?!\[bla1\]|\[/bla1\]).)+)\s*\[/bla1\]
to get everythign between bla1 and bla2, and then you can remove it
you don't need regex for that... I would use strpos, explode and substr for that functionality. further reading:
http://php.net/manual/en/function.strpos.php
http://php.net/manual/en/function.substr.php
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/function.str-replace.php
for example:
replace "[bla]" and [/bla] for ~bla~ and than explode on ~bla~.
精彩评论