用PHP内置DOMDocument对象提取HTML字符串中的img标签(非正则表达式)
$html
是html原始字符串,$matches
是提取的img
标签src
数据
$meta = '<meta charset="utf-8">'; $doc = new DOMDocument(); libxml_use_internal_errors(true); $doc->loadHTML($meta . $html); libxml_use_internal_errors(false); $xpath = new DOMXPath($doc); $nodelist = $xpath->query("//img"); // find your image $matches = array(); foreach ($nodelist as $key => $img) { $matches[] = $img->getAttribute('src'); } $matches = array_unique($matches); //去重