我正在使用下面的代码从网页中提取网址,它的工作正常,但我想过滤它。它将显示该页面中的所有网址,但我只想要那些由“超级”字样组成的网址
$regex='|<a.*?href="(.*?)"|';
preg_match_all($regex,$result,$parts);
$links=$parts[1];
foreach($links as $link){
echo $link."<br>";
}
所以它应该仅回显超级单词所在的uls。 例如,它应该忽略url
http://xyz.com/abc.html
但它应该回应
http://abc.superpower.com/hddll.html
因为它包含url中所需的单词super
答案 0 :(得分:1)
让你的正则表达不贪婪,它应该有效:
$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";
}