我的场景是:
用户想要检查目标网址中是否存在特定(客户端)网址,并且我创建了一个简单的脚本来测试一个目标网址中的特定网址。
这是我的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>
以上脚本适用于单个目标网址,但当我尝试发布多个目标网址(通过将其转换为数组)时,我收到错误:
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";
}
}
}
我在这个剧本中落后的地方?
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
据我所知,你没有数组$内容。它应该像这样工作:
$contents = file_get_contents($arr[$i]);
if (strpos($contents, $search) === FALSE) {
echo "Not Found";
} else {
echo "Found";
}