adam
adam

Reputation: 41

Load an array variable from a file

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

Upvotes: 1

Views: 2808

Answers (5)

Ron Piggott
Ron Piggott

Reputation: 705

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

Upvotes: 0

Edgard Leal
Edgard Leal

Reputation: 2720

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.

Upvotes: 0

Mischa
Mischa

Reputation: 43298

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

Upvotes: 1

Daniel R
Daniel R

Reputation: 1429

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

// OR
$celebrities = explode('\r\n', file_get_contents('array.txt'));

Upvotes: 2

Kevin
Kevin

Reputation: 1921

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.

Upvotes: 0

Related Questions