is it possible to change a value in the middle of a string
im using fwrite, and i can image a way by storeing each line of the file then expolding the line that my target is on, changing its string then rebuilding that line then putting all the lines back in there place. is there better way to do this however,
As always, a more in-depth and specific explanation will yield a better answer. When you say change a value in the middle of a string what exactly do you mean? Can you provide a specific example? What is your "target" and how do you need to change it?
PHP provides a lot of functions for processing strings ( http://us2.php.net/manual/en/ref.strings.php ). In addition to this you can traverse a string the same way you can traverse an array (because strings are arrays).
To answer your question, yes you can change a value in the middle of a string.
Technically this is possible to do, but it is not as simple as it may appear. If you use fopen in r+ mode to open the file, the file pointer will be placed at the beginning of the file. You can then use fseek to position the file pointer wherever you would like. Think of it like using the arrow keys to move the cursor in a text editor. The only problem is, anything you write to the file will overwrite any existing bytes. For example, assume I have a text file called test.txt with contents "hello world":
PHP Code:
$fh = fopen('text.txt', 'r+');
fseek($fh, 2); //advance the pointer to the third byte
fwrite($fh, 'foo');
After the above code is execute the contents of the file will look like this:
"hefoo world"
This is exactly what would happen if when editing a text file you hit the insert key and began typing in the middle of the file.