Activity Stream
48,167 MEMBERS
6736 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1.     
    #1
    Banned

    Question replace a specific line in a php file

    Hi. I'm just a noob in php. I need to know how I can replace a specific line of text in a php file. For example, in a php file below named conf.php, I want 'used_dir' => 'files/',, which is in the third line, to be replaced with 'used_dir' => 'new_files/',. How can i do this? Any reply will be appreciated.

    PHP Code: 
    <?php
    $options 
    = array (
    'used_dir' => 'files/',
    'login' => true,
    );
    ?>
    PS: I was thinking I can apply file_get_contents() and file_put_contents() functions in this but I don't have an idea how to.
    techdaemon Reviewed by techdaemon on . replace a specific line in a php file Hi. I'm just a noob in php. I need to know how I can replace a specific line of text in a php file. For example, in a php file below named conf.php, I want 'used_dir' => 'files/',, which is in the third line, to be replaced with 'used_dir' => 'new_files/',. How can i do this? Any reply will be appreciated. <?php $options = array ( 'used_dir' => 'files/', 'login' => true, ); ?> PS: I was thinking I can apply file_get_contents() and file_put_contents() functions in this but I don't Rating: 5

  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    Pirateview.org Gfxs.org StarkWood.org
    PHP Code: 
    <?php

    //Get content of your php file.
    $buffer file_get_contents('yourphpfile.php');

    //Replace using RegEx
    $buffer preg_replace("/'used_dir' => 'files\/'/""'used_dir'=> 'new_files'"$buffer);

    //output.
    echo $buffer;

    ?>

  4.     
    #3
    Banned
    what if line no. 3 is not always 'used_dir' => 'files/', but a variable that changes? what im trying to do exactly is creating a form using another php file to edit conf.php. so i was wondering if i can do this method

    (1) chmod conf.php to 777, read and copy the file with file_get_contents()
    (2) create a new file called conf_new.php and write in it the copied content using file_put_contents()
    (3) replace line no. 3 with something else
    (4) delete conf.php with unlink(),
    (5) rename conf_new.php to conf.php

  5.     
    #4
    Member
    Website's:
    Pirateview.org Gfxs.org StarkWood.org
    it simple, just add the variable name in the regular expression!

  6.     
    #5
    Banned
    i dont want to edit that file in filemanager or ftp!

  7.     
    #6
    Member
    Website's:
    Pirateview.org Gfxs.org StarkWood.org
    dude, you donot need to edit the file manually, when adding a variable in the regEx, it will automatically be updated.

  8.     
    #7
    Member
    change the config like that and define $new when you want to use dir new_files ?

    PHP Code: 
    <?php 
    if ($new==1) {
    $options = array ( 
    'used_dir' => 'new_files/'
    'login' => true
    ); 
    }

    else
    {
    $options = array ( 
    'used_dir' => 'files/'
    'login' => true
    );

    ?>

  9.     
    #8
    Respected Member
    @NucleA and Gav0: When posting PHP code please use the [PHP] tag, it makes it easier to read. I've edited your posts and put the [PHP] tag in them.
    "Maybe this world is another planet's Hell"

  10.     
    #9
    Respected Developer
    PHP Code: 
    <?php


    $options 
    = array ( 
    'used_dir' => 'files/'
    'login' => true
    ); 
    $newname "newname"// Here enter new name u wants 
    echo preg_replace("/[a-zA-Z0-9]+/",$newname,$options['used_dir']);

    ?>
    Bots Development | Web Development | Wordpress Customization | PSD Conversion

    Life has many twists and turns in it, you have to take what you are given and use it for the best.

  11.     
    #10
    Respected Developer
    Am I the only one that has noticed the real problem? Why would you hard-code settings in the first place. Editing PHP files is not how you should change settings. Either use a database or plain text (XML?) to store and alter settings. You don't need to travel trough China in order to cross the street. Programming is all about logic. Think logically .

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Delete specific file in each folder
    By Ruriko in forum Technical Help Desk Support
    Replies: 6
    Last Post: 6th Oct 2012, 01:05 AM
  2. [Blogger] Code to live replace specific words
    By nICEsHARE in forum Web Application/Script Support
    Replies: 0
    Last Post: 2nd Aug 2011, 01:01 PM
  3. Debian - command line. Replace () in filenames with _
    By AndroidApps in forum Web Development Area
    Replies: 2
    Last Post: 9th Jul 2010, 09:30 AM
  4. Specific File Hosts Sites
    By DeLeTeD in forum Forum and DDL Discussion
    Replies: 13
    Last Post: 5th Apr 2010, 10:10 PM

Tags for this Thread

BE SOCIAL