统计html页面中图片和alt标签的数量。
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
echo $tag->getAttribute('alt')."<br/>";
}
echo "<br/>".$tags->length ."'n"."images found and"."'n" .$tag->length."'n"."alt tags found"."<br/><br/>";
显然img
和alt
的数量应该相同。你是说"分开数"吗?如果是,请参见下面:
$tags = $doc->getElementsByTagName('img');
echo "There are ".count($tags)." images. ";
$alttags = 0;
foreach ($tags as $tag) {
$alt = $tag->getAttribute('alt');
if($alt != '') $alttags++;
}
echo "There are ".alttags." alt tags found";
这是你想要的吗?
可以使用preg_match
$contents=file_get_contents("http://www.google.com");
$count=preg_match_all("@<img [^>]+>@", $contents, $matches);
echo $count;
print_r($matches);
?>