如何重复此功能,直到找到我正在寻找的图像类?假设img.header仅存在于给定的一些随机id范围内。请不要使用标题。
<?php
include('simple_html_dom.php');
$randomID = mt_rand(100, 1000);
$url = "http://newspaper.com/article/".$randomID;
// Create DOM from URL or file
$html = file_get_html($url);
//Find img
$element = $html->find('img.header');
//Check if image was found
if (strpos($element,'img') == false) {
///////////////////////////////////////////
//Repeat until find('img.header') is true//
///////////////////////////////////////////
} else {
echo $element->src;
}
?>
答案 0 :(得分:0)
许多可能的方式,一个更简单的例子:
<?php
$ids = range(100, 400000); // RAM is so cheap
shuffle($ids); // still not convinced it makes a difference, but why not...
foreach( $ids as $index=>$id ) {
// <-- maybe you want to add some output here, so you can see that the script is still running.
$url = "http://newspaper.com/article/".$id;
$html = file_get_html($url);
// edit: <-- some error handling here. See documentation of simple_html_dom on how to check $html for error conditions
$element = $html->find('img.header');
if ( $element ) {
// found it
// and the ids you've had to visit before finding the element are
$visited = array_slice($ids, 0, $index+1);
break; // no need to go on, exit foreach-loop
}
}
if ( !isset($visited) ) {
// the element was never found....
}
else {
// $html, $element and even $id should be still valid
// so, you can use them here for further processing
}