Page 1 of 1

read and echo a specific column in a tab delimited text file

Posted: Tue Sep 22, 2009 8:08 pm
by greedyisg00d
How can I read and echo a specific column in a tab delimited text file. For example:

|Company Name | Address | Owner|

Above I only want to display all the data under Company Name column

Any idea? Thanks

Re: read and echo a specific column in a tab delimited text file

Posted: Wed Sep 23, 2009 12:50 am
by Robert07
You can loop through the file and echo the company name like this:

Code: Select all

 
// Get a file into an array.  
$lines = file('/path/to/your/file.txt');
 
// Loop through our array, pull out the first element and print it
foreach ($lines as $line_num => $line) {
  $lineArr = explode("|",$line);
  echo "Company name is ".$lineArr[1]."<br>"; //it is 1, not 0, because the line starts with |
}
 
Regards,
Robert