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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
greedyisg00d
Forum Commoner
Posts: 42
Joined: Thu Feb 12, 2009 2:48 am

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

Post 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
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

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

Post 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
Post Reply