[ACCEPTED]-How to get text from file into array in php-php

Accepted answer
Score: 22

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);
Score: 1
$fileArr = file("yourfile.txt")

http://www.php.net/manual/en/function.file.php

0

Score: 0

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

Score: 0
$lines = file('file.txt');

Documentation

0

Score: 0

You can use file().

<?php
$file_arr = file(/path/file);
foreach ($lines as $line_num => $line) {
    echo "Line #{$line_num}: " . $line;
}
?>

php.net file()

0

More Related questions