Sunday, 15 September 2013

regex - Negative lookup preg_match_all in PHP for IMG tags without ALT attribute -



regex - Negative lookup preg_match_all in PHP for IMG tags without ALT attribute -

i'm trying find img tags don't contain alt attribute, prepare w3c validation.

i'm trying utilize negative look-around syntax:

preg_match_all('@<img[^>]*?(?!alt=)[^>]*>@', $text, $matches);

which unfortunately doesn't homecoming anything, while there img tags without alt attribute sure.

i thought problem in negative lookup, illustration used:

preg_match_all('@<img[^>]+?http:\/\/(?!mysite\.com)[^>]*?>@', $text, $matches);

to search images external resources , worked fine.

any ideas what's wrong first expression? thanks!

using look-ahead syntax works first one

preg_match_all('@<img[^>]*?(?=alt=)[^>]*>@', $text, $matches);

$matches here contain img's alt's.

<?php $str = <<<eof <html> <body> hello <img src="withalt" alt="hi"/>asdf <img src="noalt" /> <img src="withalt2" alt="blah" /> </body> </html> eof; if (preg_match_all('@<img[^>]*?(?=alt=)[^>]*>@', $str, $matches)) { echo "matches\n"; print_r($matches); } ?>

php regex html-parsing preg-match-all regex-negation

No comments:

Post a Comment