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 />";
}

Link to comment
Share on other sites

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... :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.