Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP上一个文件内容的数据上的多个文件内容_Php_Foreach_File Get Contents - Fatal编程技术网

PHP上一个文件内容的数据上的多个文件内容

PHP上一个文件内容的数据上的多个文件内容,php,foreach,file-get-contents,Php,Foreach,File Get Contents,我发现这段代码用于检查URL上的链接 <?php $url = "http://example.com"; $input = @file_get_contents($url); $dom = new DOMDocument(); $dom->strictErrorChecking = false; @$dom->loadHTML($input); $links = $dom->getElementsByTagName('a'); foreach($links as $li

我发现这段代码用于检查URL上的链接

<?php
$url = "http://example.com";
$input = @file_get_contents($url);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
@$dom->loadHTML($input);
$links = $dom->getElementsByTagName('a');
foreach($links as $link) {
   if ($link->hasAttribute('href')) {
      $href = $link->getAttribute('href');
      if (stripos($href, 'shows') !== false) {
         echo "<p>http://example.com" . $href . "</p>\n";
      }
   }
}

?>

工作正常,它显示所有包含“shows”的链接。 例如,上面的脚本找到3个链接,因此我得到:

<p>http://example.com/shows/Link1</p>
<p>http://example.com/shows/Link2</p>
<p>http://example.com/shows/Link3</p>
http://example.com/shows/Link1

http://example.com/shows/Link2

http://example.com/shows/Link3

现在,我要做的是检查我刚刚获取的URL中是否有包含“shows”的链接

老实说,我是一个php noob,所以我不知道从哪里开始:(

问候,, 巴特

类似于:

function checklinks($url){
$input = @file_get_contents($url);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
@$dom->loadHTML($input);
$links = $dom->getElementsByTagName('a');
foreach($links as $link) {
   if ($link->hasAttribute('href')) {
      $href = $link->getAttribute('href');
      if (stripos($href, 'shows') !== false) {
         echo "<p>" . $url . "/" . $href . "</p>\n";
         checklinks($url . "/" . $href);
      }
   }
}
}

$url = "http://example.com";
checklinks($url);
函数检查链接($url){
$input=@file\u get\u contents($url);
$dom=新的DOMDocument();
$dom->strigerrorchecking=false;
@$dom->loadHTML($input);
$links=$dom->getElementsByTagName('a');
foreach($links作为$link){
如果($link->hasAttribute('href')){
$href=$link->getAttribute('href');
if(stripos($href,'shows')!==false){
回显“”$url./“$href.

\n”; 检查链接($url./“$href); } } } } $url=”http://example.com"; 检查链接($url);

使其递归-在函数本身中再次调用函数。

此脚本如何运行?按需,在计划任务/cron上运行?注意:如果您深入钻研第一页,那么您将取消并没有存储和检查重复链接,那么您很容易陷入无限循环,从而导致您的脚本使服务器陷入困境让你自己被封锁。。