我有一些代码可以获取页面的所有链接,但有些代码的链接看起来不像链接。例如,索引0-4获取名为“javascript:void(0)”的链接,索引5获得仅带有“/”的空白链接。我该如何解决?谢谢。
$content = file_get_contents("http://bestspace.co"); //get content of page
$links = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; //set regular expression to get links
preg_match_all("/$links/siU", $content, $matches); //get all links on page and store in array $matches[2]
print_r($matches[2]);
数组的内容
Array (
[0] => javascript:void(0)
[1] => javascript:void(0)
[2] => javascript:void(0)
[3] => javascript:void(0)
[4] => javascript:void(0)
[5] => /
[6] => /bestdeals
[7] => /about-us
[8] => /why-choose-us
[9] => /products
[10] => https://cloud.bestspace.co/clientarea.php
ect... );
答案 0 :(得分:2)
使用array_filter
删除所有Javascript链接。
$links = array_filter($matches[2], function($x) {
return substr($x, 0, 11) != 'javascript:';
});