Scriptman Scriptman - 3 months ago 15
PHP Question

preg_match_all except if tag has class

I have a regex which replaces shortcodes in my texts.

[test_code]

<textarea class="ignoreShortCodes">[test_code]</textarea>


Now both
[test_code]
blocks are going to replaced, but I don't want this.

Now here is my regex:

/\[([a-zA-Z0-9_-]+)\s?([^\]]*)\]/is


Please note, a shortcode can contain arguments like:

[test_code name="test"]


Who can help me with this issue?

Answer

You can use a negative lookahead:

/^(?!.*ignoreShortCodes).*\\[([a-zA-Z0-9_-]+)\\s?(?:[^\\]]*)\\]/im

which will match every lignes that doesn't contain the class ignoreShortCodes