I have this regex pattern /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/i
that I use to get e-mail addresses from a string. But now I'd like to get only all e-mail addresses that are a value of an arbitrary HTML-element attribute including the attribute itself. Have a look at my example and everything should be clear:
<?php
$subject = 'abc dont@get.me 123 <input value="please@get.me">xyz';
$pattern = '/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/i';
preg_match_all( $pattern, $subject, $matches );
var_dump( $matches );
will produce something like:
array(1) { [0]=> array(2) {
[0]=> string(11) "dont@get.me"
[1]=> string(13) "please@get.me"
} }
but I need:
array(1) { [0]=> array(1) {
[0]=> string(13) "value="please@get.me""
} }
Please be aware that <input value="please@get.me">
is just is an example. I need a pattern that can handle "all" HTML-elements with "all" attributes (I put 'all' in quotes to make clear that I'm aware that there could be some edge cases the pattern could fail because HTML isn't regular) and:
<?php
$subject = "<br data-xyz=please@get.me /> dont@get.me <[tag] [attr]='[pre] andPlease@get.me [ap]'>";
preg_match_all( $pattern, $subject, $matches );
var_dump( $matches );
should produce something like:
array(1) { [0]=> array(2) {
[0]=> string(13) "data-xyz=please@get.me"
[1]=> string(13) "[attr]='[pre] andPlease@get.me [ap]'"
} }
To be honest I'm really bad at regex patterns so I don't have a clue about how to achieve it. Hope somebody can help me out with this!
EDIT: Another solution than regex would be also totaly fine!