Entry
How can I read an ASCII textfile delimited by pipes into an array?
Jul 16th, 2001 12:52
Rich Cavanaugh, Jon Hawkins,
Two ways I can think of. First, take a look at the fgetcsv() function.
You could use this with the | as it's delimeter. Also you could read
the entire file into an array (each element is one line) using the
file() function then loop over it explode()ing on |
Example:
<?php
$myfile = 'myfile.txt';
$lines = file($myfile);
$data = array();
foreach ( $lines as $val ) {
$data[] = explode('|', $val);
}
// the following line would echo the first column from the third row
echo $data[2][0];
?>