php - How to strip all img tags from a text, except for those containing a certain word -
i strip img tags text, except contain keyword (e.g. domain they're hosted at).
here's i've come with, i'm afraid doesn't work:
$text = preg_replace("/<img[^>]+(?!keyword)[^>]+\>/i", "", $text);
any appreciated! :)
use callback simplify task:
$html = preg_replace_callback('/<img\s[^>]+>/i', "cb_keyword", $html); function cb_keyword($matches) { // return empty str or original text return !strpos($matches[0], "keyword") ? "" : $matches[0]; }
if working on html snippets using phpquery/querypath still possible, adds more post-processing.
Comments
Post a Comment