我正在使用下面的代码从网页中提取URL,它的工作正常,但我想对其进行过滤。它会显示该页面中的所有网址,但我只希望包含“ super”一词的网址
$regex='|<a.*?href="(.*?)"|';
preg_match_all($regex,$result,$parts);
$links=$parts[1];
foreach($links as $link){
echo $link."<br>";
}
因此,它应该仅在出现单词super的地方回应uls。例如,它应该忽略网址
http://xyz.com/abc.html
但它应该回声
http://abc.superpower.com/hddll.html
因为它由url中必需的单词super组成
使您的正则表达式不贪心,它应该可以工作:
$regex = '|<a.*?href="(.*?super[^"]*)"|is';
但是,要解析和废弃HTML,最好使用php的DOM解析器。
$request_url ='1900girls.blogspot.in/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($result); // loads your html
$xpath = new DOMXPath($doc);
$needle = 'blog';
$nodelist = $xpath->query("//a[contains(@href, '" . $needle . "')]");
for($i=0; $i < $nodelist->length; $i++) {
$node = $nodelist->item($i);
echo $node->getAttribute('href') . "\n";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句