PHP load array from file foreach
I have this code:
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);
And instead of
$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($handl开发者_开发百科e);
I want to implement
$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);
now the result.txt doesnt gets written anymore, where do i go wrong? the script should read an array from a file, process the function hot and then write each reasults in a new line in result.txt thanks in advance!
Assuming you have a single celeb per line:
$celebrity = trim(fgets($read_file));
As fgets
will return a string including the newline character.
On the line:
if(hot($celebrity)) { fwrite($handle, "{$celebrity}\r\n"); }
You use $handle
, which is no longer defined. You want to be using $write_file
instead:
if(hot($celebrity)) { fwrite($write_file, "{$celebrity}\r\n"); }
精彩评论