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);
}
?>
$celebrities = file('array.txt'); // possibly add an array_filter()
// OR
$celebrities = explode('\r\n', file_get_contents('array.txt'));
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);
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.
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.
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'] );
精彩评论