Regular expression, how to find all tags A which do not contain tag IMG inside it?

Posted by Kirzilla on Stack Overflow See other posts from Stack Overflow or by Kirzilla
Published on 2010-05-24T09:58:08Z Indexed on 2010/05/24 10:01 UTC
Read the original article Hit count: 172

Filed under:
|

Hello,

Let's suppose that we have such HTML code. We need to get all <a href=""></a> tags which DO NOT contain img tag inside it.

<a href="http://domain1.com"><span>Here is link</span></a>
<a href="http://domain2.com" title="">Hello</a>
<a href="http://domain3.com" title=""><img src="" /></a>
<a href="http://domain4" title=""> I'm the image <img src="" /> yeah</a>

I'm using this regular expression to find out all links

preg_match_all("!<a[^>]+href=\"?'?([^ \"'>]+)\"?'?[^>]*>(.*?)</a>!is", $content, $out);

I can modify it

preg_match_all("!<a[^>]+href=\"?'?([^ \"'>]+)\"?'?[^>]*>([^<>]+?)</a>!is", $content, $out);

But how can I tell to exclude results containing <img substring inside of <a href=""></a>?

Thank you

© Stack Overflow or respective owner

Related posts about regex

Related posts about regex-negation