我有这样的代码:
require("class.XMLHttpRequest.php");
function hot($news){
$url="https://localhost/search.aspx?search=".$news."";
$ajax=new XMLHttpRequest();
$ajax->setRequestHeader("Cookie","Cookie: host");
$ajax->open("GET",$url,true);
$ajax->send(null);
if($ajax->status==200){
$rHeader=$ajax->getResponseHeader("Set-Cookie");
if(substr_count($rHeader, "Present!")>0) { return true; }
}else{ return false; }
}
$celebrities = array('britney','gaga','carol');
$filename = 'result.txt';
$handle = fopen($filename, 'a');
foreach($celebrities as $celebrity)
{
if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); };
}
fclose($handle);而不是
$celebrities = array('britney','gaga','carol');
$filename = 'result.txt';
$handle = fopen($filename, 'a');
foreach($celebrities as $celebrity)
{
if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); };
}
fclose($handle);我想要实现
$read_file = fopen('array.txt', 'r');
$write_file = fopen('result.txt', 'a');
while(!feof($read_file))
{
$celebrity = fgets($read_file);
if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }
}
fclose($write_file);
fclose($read_file);现在result.txt不再被编写了,我哪里错了?脚本应该从文件中读取数组,热处理函数,然后在result.txt中将每个结果写在新行中,谢谢!
发布于 2010-09-30 23:49:52
假设每行只有一个名人:
$celebrity = trim(fgets($read_file));因为fgets将返回一个包含换行符的字符串。
发布于 2010-09-30 23:50:01
在线上:
if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }您使用的是不再定义的$handle。您希望改用$write_file:
if(hot($celebrity)) { fwrite($write_file, "{$celebrity}\r\n"); }https://stackoverflow.com/questions/3832304
复制相似问题