TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Adding content to a specific line in file (http://www.talkphp.com/absolute-beginners/3461-adding-content-specific-line-file.html)

Normo 10-09-2008 08:52 AM

Adding content to a specific line in file
 
I'm relatively new to PHP so bare with me.I'm trying to replace a specific line to a file. This is my code so far:
PHP Code:

$string "Blah blah blah
<!-- comment -->"
;
$file fopen("file.txt""r"); // Open file

if($file){
while(!
feof($file)){  // Before end of file
$read fgets($file1000);  // read line
if ($read=="<!-- comment -->"){ // if right line
$read $string// change to variable
}}
fclose($file);


I'm unable to test this at the moment, so if someone could check this and/or suggest anything better that would be helpful.

Cheers.

sketchMedia 10-09-2008 09:27 AM

Half way there, at the moment your script should read each line, but it wont write to file.
How about this:
PHP Code:

<?php
$szFile
'file.txt'// File we want to manipulate
$szFind '<!-- comment -->'// Our search
$szReplace "Blah blah blah\r\n<!-- comment -->"// Our replace string

$szFileText file_get_contents($szFile); // Get file the files contents
$pFh fopen($szFile'w+'); // Open a file handle
$szFileText str_replace($szFind$szReplace$szFileText); // Do string replace
fwrite($pFh$szFileText); // Write text to file
fclose($pFh); //Close handle


Normo 10-09-2008 02:48 PM

Ah thanks boy, that works a treat!

Tanax 10-09-2008 04:43 PM

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!');

    }

}

Wildhoney 10-09-2008 06:22 PM

I knew I had written a function with the exact same name! Thief :-(

Tanax 10-09-2008 11:20 PM

Yea, it was a great function, I'm still using it! However I had to modify it for this cause though. Sorry xD


All times are GMT. The time now is 02:27 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0