用PHP内置DOMDocument对象提取HTML字符串中的img标签(非正则表达式)

后端开发PHP 781

$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);  //去重

Post Comment