Jump to content

Get line number (text file)?


zeeshan_haider000

Recommended Posts

Hi,

I have a text file such as:

Tab1a||Tab2a||Tab3a
Tab1b||Tab2b||Tab3b

 

Now when i display this data, i want to display the line number next to it (i.e. Tab1b... line 2)

How would i do that?

Thanks

 

One way would be:

 

$rows = @file("/path/to/filename.ext");
foreach($rows as $key=>$value){
$row = $key+1;
echo $value." line ".$row."<br />";
}

Thanks for replying guys, I was asking the wrong question, here is my code...

$file = file($file);
$linenum = count($file);

foreach($file as $line){
$tabs = explode('||', $line);
/* Get the total lines in the file */
$no = count($tabs);
for ($i = 0; $i < $no; $i++){ echo $tabs[$i].$linenum.'<br/>'; }

instead of echoing $linenum, i was echoing $i... :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.