views:

83

answers:

3

i have the following code:

which uses an array to write the result to file

i want to create another array to read the celebities aray from another file

<?php
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 i would like to load the $celebrities array from a file instead of

$celebrities = array('britney','gaga','carol');

i couldnt get to implement this one here: what am i doing wrong ?

<?php
$handle = @fopen('array.txt', "r"); 
if ($handle) { 
   while (!feof($handle)) { 
       $celebrities[] = fgets($handle, 4096); 
   } 
   fclose($handle); 
} 
?>
A: 

Since you're separating each array entry by a newline/carriage return, you should read in the contents of the file line by line and assign each to your array, which it looks like you're doing. Perhaps declaring the array before you use it will help, i.e. $celebrities = array(); before your loop. Otherwise it looks like $celebrities is getting redefined every time your loop iterates.

If you provide more information on what is not working (parse error? contents of the array?) then I could provide a more detailed answer.

If the code doesn't give an error, print out the contents of the array print_r($celebrities); and show us your output.

Kevin
"$var" and "{$var}" is the same in PHP.
Yorirou
whoops.. looks like my PHP is a little rusty, thanks.
Kevin
+1  A: 

I don't see any problem with your code. What exactly doesn't work? Any error messages?

Why are you reading the file into an array? My suggestion:

$read_file = fopen('array.txt', 'r');
$write_file = fopen('result.txt', 'a');

while(!feof($read_file))
{
    $celebrity = trim(fgets($read_file));
    if(hot($celebrity)) { fwrite($write_file, "{$celebrity}\r\n"); }
}

fclose($write_file);
fclose($read_file);
captaintokyo
your ideea is great, that's actually what i want to do , load from a a file then write to another, you example doent give any errors but my result.txt is empty
adam
It did work when you were using the array?
captaintokyo
works but writes nothing to file, trying again now
adam
in your example it seems like after reading the file the info doesnt go in variable celebrity so i gues that's y nothing is written
adam
Sounds like for some reason you cannot read `array.txt`. Maybe something about file encoding, line endings or permissions. Sorry, I don't have time anymore now. I will check this question again later.
captaintokyo
i think it misses the old foreach($celebrities as $celebrity) line there,
adam
found it ! i must use $celebrity = trim(fgets($read_file)); and also replace $handlewith $write_file
adam
Oh, yeah, stupid that I still had `$handle` in there! Thanks.
captaintokyo
+1  A: 
$celebrities = file('array.txt'); // possibly add an array_filter()

// OR
$celebrities = explode('\r\n', file_get_contents('array.txt'));
Inigoesdr
both give empty result
adam
I used the wrong file from your question, you probably want result.txt, but it was really only meant as an example. If you have one celebrity per line in a file either should work.
Inigoesdr
yeah but i want to take an array from a file then use the function and the result goes to another file
adam
If you use file('array.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); then the newline characters and blank lines will be filtered out.
GZipp