faqts : Computers : Programming : Languages : PHP : Common Problems : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

20 of 24 people (83%) answered Yes
Recently 9 of 10 people (90%) answered Yes

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];
?>