Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7874083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:42:22+00:00 2026-06-03T02:42:22+00:00

I’m trying to build a small CMS using CodeIgniter, and I need to be

  • 0

I’m trying to build a small CMS using CodeIgniter, and I need to be able to dynamically update some variables within the application/config.php

So far I did:

private function update_file ($file, $var, $var_name) {
    $start_tag = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";
    if (file_exists($file)) {
        require_once ($file);
        $updated_array = array_merge($$var_name, $var);         
        $data = $start_tag."\$".$var_name." = ".var_export($updated_array, true).";";
        file_put_contents($file, $data);
    } else {
        return false;
    }
}

Everything works just fine! The result in the config.php file will be:

<?php ...;
$config = array (
'base_url' => '',
...
...
);

But what if I would like to maintain the original config.php file format with comments, spaces and
separated declared $config[‘key’] = ‘value’ … ?

Is that possible ?


EDIT:

Thank you for your answers, very precious.
I found a slightly different solution for my needs, performing a preg_replace on the return of file_get_contents() and then write back on the file the new resulting string. File maintains the exact original clean format.

private function update_file ($file, $var, $var_name) {
    if (file_exists($file)) {
        require_once ($file);

        $contents = file_get_contents($file);
        $updated_array = array_merge($$var_name, $var);
        $search = array();
        $replace = array();

        foreach($$var_name as $key => $val) {
            $pattern = '/\$'.$var_name.'\[\\\''.$key.'\\\'\]\s+=\s+[^\;]+/';
            $replace_string = "\$".$var_name."['".$key."'] = ".var_export($updated_array[$key], true);      
            array_push($search, $pattern);
            array_push($replace, $replace_string);
        }

        $new_contents = preg_replace($search, $replace, $contents);
        write_file($file, $new_contents);
}

Maybe it requires some slight performance improvements. But this is my baseline idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:42:26+00:00Added an answer on June 3, 2026 at 2:42 am

    It is possible. I can’t find the code , but once i have written something like that. Whole idea was based on tokenizing template file and substitute values in an array, preserving key order, line numbers and comments from the template.

    [+] Found it. It’s purpose was to fill values from template that looked like this (it was much bigger of course):

    <?php
    
    $_CFG = array(
    
        // DB section
        'db_host' => 'localhost',
        'db_user' => 'root',
        'db_pass' => '',
        'db_name' => 'test',
    
        // Site specific
        'lang' => array('pl','en'),
        'admin' => 'admin@example.com',
    );
    

    And the code that was doing all the magic:

    $tokens = token_get_all(file_get_contents('tpl/config.php'));
    
    $level = -1;
    $buffer = '';
    $last_key = 0;
    $iteration = 0;
    
    foreach($tokens as $t){
        if($t === ')'){
            $iteration = 0;
            $last_key = 0;
            $level--;
        }
    
        if(is_array($t)){
            if($t[0] == T_ARRAY && strtolower($t[1]) === 'array')
                $level++;
    
            if($t[0] == T_CONSTANT_ENCAPSED_STRING){
                if($last_key){
                    if($level){
                        if(isset($new_config[$last_key][$iteration])){
                            $buffer .= var_export($new_config[$last_key][$iteration], TRUE);
                        }
                        else
                            $buffer .= 'null';
    
                        $iteration++;
                    }
                    else{
                        if(isset($new_config[$last_key]))
                            $buffer .= var_export($new_config[$last_key], TRUE);
                        else
                            $buffer .= 'null';
    
                        $last_key = 0;
                    }
                }
                else{
                    $buffer .= $t[1];
                    $last_key = trim($t[1],"'");
                }
            }
            else
                $buffer .= $t[1];
        }
        else
            $buffer .= $t;
    }
    
    file_put_contents('config.php',$buffer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.