So, I'm using fwrite, but I want the text to go on a certain line without overwriting text.
How do I do this?
Offline
jvvg wrote:
You can process the text using something else and write it to the file when it's done processing.
Uh whut.
Offline
funelephant wrote:
jvvg wrote:
You can process the text using something else and write it to the file when it's done processing.
Uh whut.
By that, I mean read the file, do something with the text independent of the file I/O functions, and then put the result in the file.
Offline
May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions
Offline
SJRCS_011 wrote:
May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions
However, I think funelephant is new to PHP, and files are a lot easier to use to get started (my first PHP projects, and even most of Mod Share 3 used files).
Last edited by jvvg (2012-10-18 21:34:08)
Offline
jvvg wrote:
SJRCS_011 wrote:
May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptionsHowever, I think funelephant is new to PHP, and files are a lot easier to use to get started (my first PHP projects, and even most of Mod Share 3 used files).
Yes. Yes I am new
Offline
SJRCS_011 wrote:
May I ask why you're using fwrite in the first place?
If you're trying to store data, then databases are much easier and much more secure than files, with some exceptions
Actually, if you store the files in a place that isn't accessible through normal means (HTTP/web server), say, the directory above public_html (think is the folder name), it's just about as secure as databases, though not encrypted (unless you write a program to do that). I think SQL-Lite uses local files instead of a full blown database server. If you use files (instead of SQL), you can avoid SQL-Injections. Though you have to make sure your code doesn't have any ways to have code injected into it.
Offline
So what the guys above me are trying to suggest, is that you use fopen and process the lines of the files into an array. Then using standard php string functions, you can do your processing. Finally, just write the array back into the file.
Offline
Are you trying to insert text into a line of an existing multiline text string? You could try using this code.
function addline($inputString, $line, $insertString){ //a function that inserts the string $insertString on a new line below the specified line number $line, in the string $InputString and returns the result. If the line number given is greater than the number of lines in the input string $inputString, it will add it at the bottom. $max = strlen($inputString); //make sure that the search system doesn't keep searching further than the actual length of the input $n = 0; //keeps track of the line count $c = 0; //keeps track of the character count for($i=0;$i<$max;$i++){ //loop through every character in your input string $c++;//increment the character counter by 1 if($string[$i]=='\n'){ $n++; // if the character equals a newline, change $n by 1. } if($n == $line){ break; // if the line number reached is equal to the line you're looking for, leave the counting for loop. } } return substr($inputString, 0, $c) . $insertString . '/n' . substr($inputString, $c + 1, $max); // join together the section of the input string before the line you want to insert at, the string to input followed by a newline and then the other half of the input string and return this. }
I hope this is vaguely understandable, I've commented the code (though I've not tried it out).
Further reading:
substr()
strlen()
for loops
functions in PHP
Offline
sparks wrote:
Are you trying to insert text into a line of an existing multiline text string? You could try using this code.
Code:
function addline($inputString, $line, $insertString){ //a function that inserts the string $insertString on a new line below the specified line number $line, in the string $InputString and returns the result. If the line number given is greater than the number of lines in the input string $inputString, it will add it at the bottom. $max = strlen($inputString); //make sure that the search system doesn't keep searching further than the actual length of the input $n = 0; //keeps track of the line count $c = 0; //keeps track of the character count for($i=0;$i<$max;$i++){ //loop through every character in your input string $c++;//increment the character counter by 1 if($string[$i]=='\n'){ $n++; // if the character equals a newline, change $n by 1. } if($n == $line){ break; // if the line number reached is equal to the line you're looking for, leave the counting for loop. } } return substr($inputString, 0, $c) . $insertString . '/n' . substr($inputString, $c + 1, $max); // join together the section of the input string before the line you want to insert at, the string to input followed by a newline and then the other half of the input string and return this. }I hope this is vaguely understandable, I've commented the code (though I've not tried it out).
Further reading:
substr()
strlen()
for loops
functions in PHP
Thanks! I'll fix it later today
And Happy Birthday!
Offline