1

我的场景是:

用户想要检查目标 url 中是否存在特定(客户端)url,我创建了简单的脚本来测试一个目标 url 中的特定 url。

这是我的 php 脚本:

if(isset($_POST['check_url']))
{
    $client_url = $_POST['client_url'];
    $destination_url = $_POST['destination_url'];
    $contents = file_get_contents($destination_url);
    $search   = $client_url;

    if(strpos($contents,$search)== FALSE)
    {
        echo "Not Found";
    }
    else
    {
        echo "Found";
    }
}

这是我的html脚本:

<form method="post" action="test.php">
<label>Client URL:</label>
<input type="text" value="" name="client_url" /><br />
<label>Destination URL:</label>
<textarea value="" name="destination_url" ></textarea><br />
<button type="submit" name="check_url">Check</button>
</form>

上面的脚本在单个目标 url 的情况下工作,但是当我尝试发布多个目标 url(通过将其转换为数组)时,我得到了错误:

Warning: file_get_contents( http://learntk12.org/story.php?title=seo-link-building-service) [function.file-get-contents]: failed to open stream: No such file or directory in "path to source file" on line 24

其中第 24 行是:$contents[$i] = file_get_contents($arr[$i]);

这是我的带数组的 php 代码:

if(isset($_POST['check_url']))
{
    $client_url = $_POST['client_url'];
    $destination_url = $_POST['destination_url'];
    $destination =str_replace("\r",",",$destination_url);
    $arr = explode(",",$destination);

    $search   = $client_url;

    for($i=0;$i<count($arr);$i++)
    {
        $contents[$i] = file_get_contents($arr[$i]);

        if (strpos($contents[$i], $search) === FALSE)
        {
            echo "Not Found";
        }
        else
        {
            echo "Found";
        }
    }


}

我在这个脚本中落后于哪里?

4

2 回答 2

1

试试这个:

if (isset($_POST['check_url']))
{
    $client_url = $_POST['client_url'];
    $destination_url = $_POST['destination_url'];
    $destinations = str_replace(array("\r\n", "\n"), ',', $destination_url); // replace both types of line breaks with a comma, \r alone will not suffice
    $destinations = explode(',', $destinations); // split at commas

    foreach ($destinations as $destination) // loop through each item in array
    {
        $contents = file_get_contents($destination); // get contents of URL

        echo (FALSE === strpos($contents, $client_url)) // check if remote data contains URL
            ? 'Not Found' // echo if TRUE of above expression
            : 'Found'; // echo if FALSE of above expression
    }
}
于 2013-01-02T07:38:12.293 回答
0

据我所知,您没有数组 $contents. 它应该像这样工作:

$contents = file_get_contents($arr[$i]);
if (strpos($contents, $search) === FALSE) {
   echo "Not Found";
} else {
   echo "Found";
}
于 2013-01-02T07:37:57.940 回答