I want to exclude a class from this regex, for example "apple" or "apple-tree"
preg_match_all('/class[~=]+[\'"](.*?)[\'"]/i', $html, $matches1, PREG_SET_ORDER, 0);
How would I achieve this in php regex?
I want to exclude a class from this regex, for example "apple" or "apple-tree"
preg_match_all('/class[~=]+[\'"](.*?)[\'"]/i', $html, $matches1, PREG_SET_ORDER, 0);
How would I achieve this in php regex?
You can do this with a negative lookahead, BUT it's a slippery slope and I recommend using something other than regular expressions.
Let's start here (removing the unrelated regex for now, just for brevity):
class="((?!apple).*?)"
This looks good at first glance; it successfully ignores classes that equal or start with apple
. But what if we want to ignore matches that have apple
anywhere in the list? Because the class attribute can have many classes in any particular order.
class="((?:(?!apple).)*?)"
Nice, that takes care of excluding a class attribute where apple
appears anywhere in the value. (More in-depth explanation of this.)
But, crap, our important customer Snapple reported a bug where their custom class names aren't working anymore.
You can see where this is going.
It's very possible that you have a more controlled scenario and one of the above will be useful. If so, awesome! I hope this helps.
For some alternatives to trying to do this by regular expressions, consider:
DOMDocument
preg_replace_callback()
, where you can add in some business logic around a more generic matching pattern