今天我使用PHP DOMDocument类来查找源代码中的所有链接。 $ links数组包含来自网站的所有链接。函数'for'使用循环迭代来查找给定的$域。PHP - 在循环中显示'echo'一次
echo 'Find link: ' . $domain . '';
echo "<b>Status: ";
//$links is array with all links
//$domain is domain for example : http://example.com
for($i = 0;$i<count($links);$i++)
{
$find_href = preg_match("@[email protected]", $links[$i]['href']);
if($find_href)
{
if($links[$i]['href'] != "")
{
echo $links[$i]['anchor'];
echo 'Link found';'
}
else
{
echo 'Link not found';
}
实施例:
搜索域:HTTP://示例.com 找遍的URL:http://www.iana.org
结果:
Link not found
Link not found
Link not found
Link not found
Link not found
http://www.iana.org/domains/example
Link found // find bacouse link is in source code of http://example.com
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
搜索域:http://example.com 已检索的链接:http://google.com
结果:
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
Link not found
如果没有在发现$域$链接阵列,如何打印一次“链接没有发现”?
你想,如果没有找到阻止你循环?或者只打印一次X消息的消息? –
print_r($ links)的输出是什么,只需要一个输出示例 – Elby
@RiggsFolly感谢您的关注 – limakg