Editing a (config) file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sonnich

    #1

    Editing a (config) file

    Hi

    I have a file with some configation in it... I am wondering how I
    correct single values in there.

    The most secure is to recreate the file, then rename it.

    But is there a way to correct a line in the text file?
    setting1=bla -setting1=blabla bla

    Needless to say, there are things in there I'd like to keep at all
    times.

    BR
    Sonnich

  • Gulasch

    #2
    Re: Editing a (config) file

    Sonnich schrieb:
    Hi
    >
    I have a file with some configation in it... I am wondering how I
    correct single values in there.
    >
    The most secure is to recreate the file, then rename it.
    >
    But is there a way to correct a line in the text file?
    setting1=bla -setting1=blabla bla
    >
    Needless to say, there are things in there I'd like to keep at all
    times.
    >
    BR
    Sonnich
    >
    You could use preg_replace() (have a look at php.net).

    For your Example it could be

    --snip--
    //Read the contents of the file into $file

    $file=preg_repl ace("'setting1\ =.*'", "setting1=blabl abla", $file);

    //Write $file back to the file.
    --snap--

    Or do I misunderstand your question?

    Comment

    • David Haynes

      #3
      Re: Editing a (config) file

      Sonnich wrote:
      Hi
      >
      I have a file with some configation in it... I am wondering how I
      correct single values in there.
      >
      The most secure is to recreate the file, then rename it.
      >
      But is there a way to correct a line in the text file?
      setting1=bla -setting1=blabla bla
      >
      Needless to say, there are things in there I'd like to keep at all
      times.
      >
      BR
      Sonnich
      I would read the file into an associative array, update the value based
      on the key, write a new file, rename it to the original. I might also
      flock() the original configuration file if the operating system does not
      support atomic rename/move.

      -david-

      Comment

      • Alvaro G. Vicario

        #4
        Re: Editing a (config) file

        *** Sonnich escribió/wrote (7 Jul 2006 01:55:03 -0700):
        I have a file with some configation in it... I am wondering how I
        correct single values in there.
        >
        The most secure is to recreate the file, then rename it.
        >
        But is there a way to correct a line in the text file?
        setting1=bla -setting1=blabla bla
        You may find parse_ini_file( ) useful:

        array parse_ini_file ( string filename [, bool process_section s] )

        parse_ini_file( ) loads in the ini file specified in filename, and returns
        the settings in it in an associative array. By setting the last
        process_section s parameter to TRUE, you get a multidimensiona l array, with
        the section names and settings included. The default for process_section s
        is FALSE


        --
        -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
        ++ Mi sitio sobre programación web: http://bits.demogracia.com
        +- Mi web de humor con rayos UVA: http://www.demogracia.com
        --

        Comment

        Working...