I asked here a while ago about matching text inside of two wrapped <code>..</code> tags from a string, and it’s been working great until somebody had some other HTML wrapped inside the <code> tags.
This is how I’m doing it so far:
preg_match_all("!<code>([^<]*)</code>!", $string, $return_array);
Could anybody improve this regular expression to solve my problem?
This is one case where I have to agree with the dreaded regex are evil meme. For straightforward extraction purposes, regular expressions are often suitable. But if you want to process malformed and or nested HTML, it’s not an option without significant fuss.
Hence I’d recommend using phpQuery or QueryPath for such occasions. It’s also pretty simple: