1

I have the following code which uses an array to write the result to file. I want to create another array to read the celebrities array 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);
?>

I would also like to load the $celebrities array from a file instead of

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

I couldnt get this to work. What am I doing wrong?

<?php
$handle = @fopen('array.txt', "r"); 
if ($handle) { 
   while (!feof($handle)) { 
       $celebrities[] = fgets($handle, 4096); 
   } 
   fclose($handle); 
} 
?>
2
  • 4
    I'd love to see the hot function. Commented Sep 30, 2010 at 14:42
  • Please don't yell at us. Commented Sep 30, 2010 at 14:48

5 Answers 5

2
$celebrities = file('array.txt'); // possibly add an array_filter()

// OR
$celebrities = explode('\r\n', file_get_contents('array.txt'));
Sign up to request clarification or add additional context in comments.

3 Comments

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.
yeah but i want to take an array from a file then use the function and the result goes to another file
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.
1

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);

6 Comments

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
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
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.
i think it misses the old foreach($celebrities as $celebrity) line there,
found it ! i must use $celebrity = trim(fgets($read_file)); and also replace $handlewith $write_file
|
0

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.

1 Comment

"$var" and "{$var}" is the same in PHP.
0

You can also do that for work in OSX linux and Windows:

$content = file_get_contents($file_name);
$array = explode(PHP_EOL, file_get_contents('fileName.txt'));

The PHP_EOL return the end of line for current OS.

Comments

0

This is how I store variables in a file:

<?php

    return array(
        'db' => array(
            'host' => '127.0.0.1',
            'port' => '3306',
            'database' => 'someDatabase',
            'username' => 'someUsername',
            'password' => 'somePassword'
        )
    );

Then I assign the variables to $MariaDB with this command:

$MariaDB = include("read.php");

This is how an example of I access the variables:

$dsh = "mysql:host=" . $MariaDB['db']['host'] . ";dbname=" . $MariaDB['db']['database'];
$dbh = new PDO($dsh, $MariaDB['db']['username'], $MariaDB['db']['password'] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.