Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text from file into array in php

Tags:

php

I have text file with some stuff that i would like to put into array. That text file has one value per line. How do i put each line into array?

like image 925
Phil Avatar asked Sep 03 '09 11:09

Phil


3 Answers

use the file() function - easy!

$lines=file('file.txt');

If you want to do some processing on each line, it's not much more effort to read it line by line 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);
like image 53
Paul Dixon Avatar answered Sep 28 '22 07:09

Paul Dixon


use file()

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

like image 34
William Macdonald Avatar answered Sep 28 '22 06:09

William Macdonald


$fileArr = file("yourfile.txt")

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

like image 20
Aaron W. Avatar answered Sep 28 '22 06:09

Aaron W.