mbellian Posted February 22, 2013 Share Posted February 22, 2013 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 https://forums.phpfreaks.com/topic/274825-php-line-numbering/ Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 Post your code. Link to comment https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414175 Share on other sites More sharing options...
shlumph Posted February 22, 2013 Share Posted February 22, 2013 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 https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414187 Share on other sites More sharing options...
Stooney Posted February 22, 2013 Share Posted February 22, 2013 $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 https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414188 Share on other sites More sharing options...
mbellian Posted February 22, 2013 Author Share Posted February 22, 2013 <?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 https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414199 Share on other sites More sharing options...
Jessica Posted February 22, 2013 Share Posted February 22, 2013 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. 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 https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414200 Share on other sites More sharing options...
shlumph Posted February 22, 2013 Share Posted February 22, 2013 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. Link to comment https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414206 Share on other sites More sharing options...
mbellian Posted February 22, 2013 Author Share Posted February 22, 2013 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 https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414207 Share on other sites More sharing options...
Psycho Posted February 22, 2013 Share Posted February 22, 2013 $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"; } Link to comment https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414213 Share on other sites More sharing options...
mbellian Posted February 22, 2013 Author Share Posted February 22, 2013 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. Link to comment https://forums.phpfreaks.com/topic/274825-php-line-numbering/#findComment-1414222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.