php - regular expression to secure email addresses -


lets there 2 default html email tags:

<a href="mailto:test@test.com">test@test.com</a> <a href="mailto:test@test.com" nosecure>test@test.com</a> 

i want find email tag without nosecure tag in php. \<a\b(?![^>]*\bnosecure\b)[^>]*>[^<]*<\/a> trick far.
want have 1 group value of href tag , 1 group text inside <a>...</a> tag. second group easy:

\<a\b(?![^>]*\bnosecure\b)[^>]*>([^<]*)<\/a> 

but how first group? there can unlimited other chars after/before href tag , nosecure can after/before href tag.
how regex group value of href="mailto:<group>". also, there can ' instead of ".

test cases , current attempt: https://regex101.com/r/rnezo3/2

thanks :)
greetings

never use regular expressions parse html. use a dom parser! easier think, have learn bit of xpath find attribute (or lack thereof) , text contents.

<?php $html = <<< html <div> <a href="mailto:test@test.com">test@test.com</a> <a href="mailto:test@test.com" nosecure>test@test.com</a> </div> html; $dom = new domdocument(); $dom->loadhtml($html); $xpath = new domxpath($dom);  /* href attribute */ $result = $xpath->query("//a[not(@nosecure)]/@href"); foreach ($result $node) {     echo str_replace("mailto:", "", $node->value); }  /* text content */ $result = $xpath->query("//a[not(@nosecure)]/text()"); foreach ($result $node) {     echo $node->textcontent; } 

Comments