Jump to content

PHp line numbering


mbellian

Recommended Posts

im no expert in php and part of my site needs to take a txt file and number each line starting with 1 to the number of lines in the file starting with the second line. I have no idea how to do this . ive tried everything. ex would be

line 1

line 2

line 3

line 4

 

TO

 

line 1

1 line 2

2 line 3

3 line 4

 

any ideas? any help is appricated.

Link to comment
Share on other sites

What are you having trouble with? Keeping track of the line numbers, reading/writing to the file, or both? As mentioned, posting your code would help.

 

I would read the file and write to a new file line by line, pre-pending the number. Easy enough, right? After you have the new file, simply rename it to the original file name you were reading from, thus replacing it.

Link to comment
Share on other sites

$data = file('file.txt');  //Reads file into array, each line has it's own element

echo $data[0].'<br />'; //Output without a line number

for($i=1; $i<=(count($data)-2); $i++){ //Starts at the second element of array
echo $i.' '.$data[$i].'<br />'; //You can change this to save into a file if necessary
}

Link to comment
Share on other sites

<?PHP

 

$c = file_get_contents("$myFile");

$e = explode("\n ",$c);

foreach ($e as $el => $es) {

$el++;

$fh = fopen($myFile, 'w+') or die("can't open file");

$write = ($el." ".$es);

fwrite($fh, $write);

print($el." ".$es);

}

?>

 

heres what i tried using doesnt seem to do exactly as i need

Link to comment
Share on other sites

1. Don't put a variable in quotes for no reason. (line 1)

2. Please use code tags when you post.

 

So now, post the input you're giving it and the output you get. :psychic:

 

The first thing to try is removing the space after your \n.

You also never add the \n back at the end of the lines.

Link to comment
Share on other sites

What happens if one of your lines contains the string "\n"? This breaks your logic. I would suggest using fgets, which gets a line from the file pointer.

http://www.php.net/m...ction.fgets.php

 

Or even easier, what Stooney has already mentioned:

$data = file('file.txt'); //Reads file into array, each line has it's own element

echo $data[0].'<br />'; //Output without a line number

for($i=1; $i<=(count($data)-2); $i++){ //Starts at the second element of array
echo $i.' '.$data[$i].'<br />'; //You can change this to save into a file if necessary
}

 

Also, you want to be writing to a separate file than what you're reading from. When you are done, replace the old file with the new one.

Edited by shlumph
Link to comment
Share on other sites

this is what prints to the screen:

 

user submitted 1 16488 | | 2 | #1 | | 5.00 | | | Breakfast Specials | NYNF | KIT 3 | Sausage | 1 | 0.00 | M | | | Breakfast Specials | NYNF | KIT 4 | Over Easy | 1 | 0.00 | M | | | Breakfast Specials | NYNF | KIT 5

 

this is whats in the file:

 

5

 

and this is what I would like the text file to look like:

 

16488 | |

1 | #1 | | 5.00 | | | Breakfast Specials | NYNF | KIT

2 | Sausage | 1 | 0.00 | M | | | Breakfast Specials | NYNF | KIT

3 | Over Easy | 1 | 0.00 | M | | | Breakfast Specials | NYNF | KIT

 

im trying to integrate online ordering to a pos.

Link to comment
Share on other sites

$data = file('file.txt'); //Reads file into array, each line has it's own element

echo $data[0].'<br />'; //Output without a line number

for($i=1; $i<=(count($data)-2); $i++){ //Starts at the second element of array
echo $i.' '.$data[$i].'<br />'; //You can change this to save into a file if necessary
}

 

You shouldn't put something such as (count($data)-2) as the comparison within a for loop. That makes the server do a calculation on each iteration of the loop. Better to set a variable using count() before the loop. In any case, I think this is more efficient.

foreach(file('file.txt') as $idx => $line)
{
   echo (($idx) ? "$idx " : '') . $line . "<br>\n";
}

Edited by Psycho
Link to comment
Share on other sites

when i try stooney's example i get

Warning: file() [function.file]: Filename cannot be empty in D:\Hosting\7754912\html\new\samplephpsubmit1.php on line 98

 

It doesn't seem to be reading the file name or variable $myfile that I have set but reads and writes the text file of the info before that code. I cant use a set file name as it changes according to your name

 

nevermind that was my fault for not paying attention to capitalization on letters. the variable is $myFile and i was using $myfile

 

the code seems to be working im going to write it to a file and find out.

Edited by mbellian
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.