Preg_replace str, if you do not immediately follow str

I tried for 2 hours, but I can't properly manage regex. I would like to replace all html comments with a string, but only if the tag is immediately applied to the NOT line <br>.

I already have a job that already works well to remove all html comments from a string:

preg_replace('/<!--.*?-->/s', '', $str);

I try to add so that the comment is NOT replaced if a tag comes to him immediately <br>. I thought something like this (obviously wrong), but I can’t wrap myself around it:

preg_replace('/<!--.*?-->(?!<br>)/s', '', $str);

Any guidance is appreciated.

+4
source share
1 answer

, , , , <!--, , .*?, , -->, <br>, .

, , .*? -->. (?:(?!-->).)* , :

'~<!--[^-]*(?:-(?!->)[^-]*)*-->(?!<br>)~s'

regex

[^-]*(?:-(?!->)[^-]*)* 0+, - ([^-]*), 0+ - -> (. -(?!->)), 0+ -.

(?:(?!-->).)* (.), --> ((?!-->)), (*).

+2

Source: https://habr.com/ru/post/1653653/


All Articles