TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-09-2008, 08:52 AM   #1 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default 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.
Normo is offline  
Reply With Quote
Old 10-09-2008, 09:27 AM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-09-2008, 02:48 PM   #3 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default

Ah thanks boy, that works a treat!
Normo is offline  
Reply With Quote
Old 10-09-2008, 04:43 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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

    }

}
__________________
Tanax is offline  
Reply With Quote
Old 10-09-2008, 06:22 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I knew I had written a function with the exact same name! Thief
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 10-09-2008, 11:20 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Yea, it was a great function, I'm still using it! However I had to modify it for this cause though. Sorry xD
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:19 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design