10-09-2008, 04:43 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Here's a function for this. Haven't tested, but should work.
Pretty easy to use as the following example:
PHP Code:
$bReplace = file_intersperse_contents('file.txt', 'This is our replaced text', 'replace me');
if($bReplace) {
echo 'File has successfully been edited';
}
else {
echo 'File could not be changed, check your permissions';
}
Function:
php Code:
/** * Function file_intersperse_contents * Params $szFilename, $szInject, $szFind * * Replaces text in a file after the specified startingpoint * Returns true or false **/function file_intersperse_contents ($szFilename, $szInject, $szFind) { // Check if file exist if(file_exists($szFilename)) { // If file exist // Get the content $szContent = file_get_contents($szFilename); // Replace the text $szReplace = str_replace($szFind, $szInject, $szContent); // Open the file $szFile = fopen($szFilename, 'w+'); // Write the text into it $bWrite = fwrite($szFile, $szReplace); if($bWrite) { // If success, close file and return true fclose($szFile); return true; } // If failure, close file and return false fclose($szFile); return false; } else { die('File' . $szFilename . 'does not exist!'); }}
__________________
|
|
|
|