【问题标题】:my script is reading only first .txt file which was first read by it我的脚本只读取它首先读取的第一个 .txt 文件
【发布时间】:2012-11-27 11:03:16
【问题描述】:

我有这个脚本,它需要一个带有不同链接和一个 url 的 txt 文件。并且此脚本会检查 .txt 文件中的链接上存在的反向链接。我的脚本是这样的

<?php
$needle = $_GET['utext'];
$file = $_GET['ufile'];
$source = file_get_contents($file);
$new = explode("\n",$source);
foreach ($new as $check){
    $a = file_get_contents(trim($check));
    if (strpos($a,$needle)){
        $found[] = $check;
    }
    else{
        $notfound[] = $check;
    }
}

echo "Matches that were found: \n ".implode("\n","\n".$found)."\n";
echo "<br> <br> <br> <br>";
echo "Matches that were not found \n". implode("\n","\n".$notfound);

?>

【问题讨论】:

  • 你怎么可能相信到处张贴包含“在此处输入代码”的内容是可以接受的? :/ 请...看看编辑帮助。编辑框正上方有一个亮橙色按钮。
  • 我在@GBD,缺少实际问题。还可以将部分包装到它自己的函数中,这将使您的代码更有说服力,并且更容易处理错误情况。
  • 我的脚本只读取第一个 txt 文件。这是我的主要问题。我想。我不知道是什么问题。
  • 您的确切预期 o/p 是多少?在您的问题中粘贴var_dump($new)var_dump($source)
  • 你想要吗?你想要什么?

标签: php file-get-contents


【解决方案1】:

添加更多调试输出。 printf/echo-debugging 不是抽屉里最锋利的勺子,但它能让你开始。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$needle = $_GET['utext'];
$file = $_GET['ufile']; // that's ok regarding security?
$new = file($file, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
$new = array_map('trim', $new);
echo '#entries: ', count($new), "\n";
$found = array(); $notfound=array();

foreach ( $new as $check ) {
    echo 'processing: ', $check;
    $a = file_get_contents($check);
    echo ', length: ', strlen($a);
    if (strpos($a,$needle)) {
        echo ", found\n";
        $found[] = $check;
    }
    else {
        echo ", not found\n";
        $notfound[] = $check;
    }
}

echo '#Matches: ', count($found), "\n";
echo '#No-Matches: ', count($notfound), "\n";
echo "Matches that were found:\n ", implode("\n", $found), "\n";
echo "Matches that were not found:\n", implode("\n", $notfound), "\n";

【讨论】:

  • 感谢 VolkerK,我只是想更正我的脚本。你纠正了我。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 2010-12-26
  • 2017-02-12
  • 2022-08-21
  • 1970-01-01
相关资源
最近更新 更多