Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post

    Question How to edit certain lines in the file?

    As I said in title...

    For example, I have a file settings.php. there's:

    PHP Code:
    <?php
    some code
    $settings
    ['title'] = "Blablabla";
    some code
    ?>
    How to edit that "Blablabla" value? or that line?

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    1)Open the file.
    2)Read the contents to a buffer
    3)Use some regex or simple string replace function to make the changes.
    4)Write it back to file.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    If all of the settings are in the $settings array(i.e. you don't have any other arrays in the file like $dbsettings) you could change the settings in the script and generate a new file:
    Code:
    $settings = array('title' => 'test', 'setting1' => 'on'); // sample data, you won't need this
    
    $temp = '<?php
    
    ';
    
    foreach($settings as $k => $v)
    	$temp .= '$settings[\'' . $k . '\'] = \'' . addslashes($v) . '\';
    
    ';
    
    $temp .= '
    
    ?>';
    
    file_put_contents('config.php', $temp);
    Edit: The vB php code syntax highlighter removes newlines and screws up the highlighting when you have <?php tags in a string, so I changed the strings and used code tags. You can see the original code here.
    Last edited by Inigoesdr; 01-20-2008 at 04:13 PM.

  • Users who have thanked Inigoesdr for this post:

    M.W.S. (02-04-2008)

  • #4
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    I was trying hard, but I was unable to do it

    I want to make admin panel where I can update site settings (with inputs).
    and I want to display value of each setting in input too.
    Here's the code:
    PHP Code:
    <?php
    if( 'settings.php' == basename($_SERVER['SCRIPT_FILENAME'])){
     die (
    '<center><b>Error 404</b></center>');
    }
    else
    {
    include_once(
    'conMyDb.php');
    include_once(
    'functions.php');
    include_once(
    'lang/en.php');
    $settings['slogan'] = "Learn More...";
    $settings['charset'] = "UTF-8";
    $settings['description'] = "Free online tutorials";
    $settings['mod_rewrite'] = "0";
    $settings['max'] = "3";
    $settings['base'] = "http://localhost/cats/";
    $settings['imgDir'] = "http://localhost/cats/images/";
    }
    ?>
    and when I change language (in dropdown box) I want the script to write include_once('lang/somelang.php');

  • #5
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    Well, I was able to fix writing to the file (thanks to Inigoesdr) but I still don't know how to display those setting values in the input box. Any ideas?

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Presumably the current config file would be included so you could just set the "value" parameter of the form input to the current value of the setting:
    PHP Code:
    <input type="text" value="<?php echo htmlentities($settings['title']); ?>" />

  • Users who have thanked Inigoesdr for this post:

    M.W.S. (02-04-2008)

  • #7
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    Inigoesdr
    Thank you very much!!!
    I'm thinking to to build a CMS but I feel like a noob. Sometimes I don't know such basic functions!

    Thanks again

  • #8
    New Coder
    Join Date
    Jan 2008
    Location
    At home :p
    Posts
    39
    Thanks
    7
    Thanked 1 Time in 1 Post
    And one more question. It's slightly off topic, but still, it's related to this file: how to deal with unicode characters in that inputs? When I type some unicode characters (Ex. აააა), it writes into the target file well, without any problem, but when it's viewed in input field, it shows up some nasty characters instead of normal symbols (Like this: &#195;&#161;&#194;�&#194;�).
    I saved both documents in UTF-8 format and I tried to user htmlscpecialchars, and utf8 functions, but no use

    ***

    Fixed. I used html_entity_decode
    Last edited by M.W.S.; 02-04-2008 at 09:31 PM. Reason: fixed


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •