|
Posted by Christian Aigner on 06/15/07 17:13
On Fri, 15 Jun 2007 16:43:13 +0000, lawpoop wrote:
> And now for the reverse question -
>
> What's the best way to insert a string, or a line, in an arbitrary
> position in a text file?
How is the "arbitrary" position defined?
If it is defined as "always after string XYZ" then you can use the
same approach as in your first question.
Simple read the contents of the file into a string variable and replace
"XYZ" with "XYZ"+newstring.
Example:
$filename = "path/to/file";
$looking_for = "XYZ";
$new_string = "very important stuff";
$string_to_be_inserted = $looking_for . $new_string ;
$content = file_get_contents($filename);
$clean_content = str_replace($looking_for, $string_to_be_inserted"",
$content);
file_put_contents($filename, $clean_content);
Navigation:
[Reply to this message]
|