[ACCEPTED]-How to get text from file into array in php-php
Accepted answer
use the file() function - easy!
$lines=file('file.txt');
If you want to 3 do some processing on each line, it's not 2 much more effort to read it line by line 1 with fgets()...
$lines=array();
$fp=fopen('file.txt', 'r');
while (!feof($fp))
{
$line=fgets($fp);
//process line however you like
$line=trim($line);
//add to array
$lines[]=$line;
}
fclose($fp);
file
will return an array of the file content 3 where each element corresponds to one line 2 of the file (with line ending character 1 seqence).
You can use file().
<?php
$file_arr = file(/path/file);
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num}: " . $line;
}
?>
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.