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

  • Home
  • SEARCH
  • 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 7054633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:39:03+00:00 2026-05-28T03:39:03+00:00

I was working on a dynamic way to update a config.php file and I

  • 0

I was working on a dynamic way to update a config.php file and I ran into an interesting glitch that I can’t quite solve. Below is my code for updating the config.php file:

if( isset( $_POST['submitted'] ) ) {

    $config_keys = array();
    foreach( $_POST as $key => $value ) {
        if( substr( $key, 0, 7 ) == 'config-' ) {
            $config_keys[ substr( $key, 7 ) ] = $value;
        }
    }

    $config_content = file_get_contents( dirname(__FILE__) . '/../../inc/config.php' );

    foreach( $config_keys as $key => $value ) {

        $config_content = preg_replace(
                                "/config\['$key'\](\s*)=(\s*)(['\"]?).*?(['\"]?);/",
                                "config['$key']$1=$2$3$value$4;",
                                $config_content
                            );

        $config[$key] = $value;

    }

    file_put_contents( dirname(__FILE__) . '/../../inc/config.php', $config_content );

}

The logic is fairly sound. It searches for any POST variables prefixed by “config-” then uses everything after “config-” as the name of the key in our config file to update. The config file takes the form:

$config['var1'] = 'value1';
$config['var2'] = 123;
$config['var3'] = '...';

In 90% of cases this works perfectly, however if $value begins with a numeral then $3 and the first numeral of $value are completely ignored during the replacement.

For instance, I have the following value in my config file:

$config['ls_key'] = '136609a7b4....'; // Rest of key has been truncated

If I don’t change this value and leave the key untouched but submit my form then this line suddenly looks like so:

$config['ls_key'] = 36609a7b4...'; // Rest of key has been truncated

The lack of single quote prevents the config file from parsing (breaking the entire site) and we’ve lost data to boot! After reading the PHP preg_replace manual I have tried using braces in several locations (modifying “Example #1 Using backreferences followed by numeric literals“). None of the following worked:

"config['$key']$1=$2${3}$value$4;",
"config['$key']$1=$2$3${value}$4;",
"config['$key']$1=$2$3{$value}$4;",
"config['$key']$1=$2{$3}$value$4;", // This one actually leads to syntax errors
"config['$key']${1}=${2}${3}$value${4};",

The first 3 lead to the exact same problem, having no effect on the replacement. The fourth doesn’t work at all (syntax errors), and the fifth actually causes EVERY backreference to be ignored. I’ve also tried using single quotes and concatenation like so:

'config[\'$key\']$1=$2$3' . $value . '$4;',

Again, I had the same problem as the 3 prior examples and my original script.

Hoping someone has solved this before or at least has a new 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-05-28T03:39:03+00:00Added an answer on May 28, 2026 at 3:39 am

    Seems the double quoting interpolation is messing things up. This replacement works:

    'config[\''.$key.'\']$1=$2${3}'.$value.'$4;'
    

    Also note that you should properly escape the following (meta characters):

    • $key in the regex with preg_quote
    • $key and $value in the replacement, there is no built in function to do this (preg_quote escapes too much)

    And also escape the regex delimiter and quote delimiter used if present.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a PHP dynamic web page that has a <textarea> element that
I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
We're working in a Dynamic Data project that will handle entities coming from two
I'm working on a stored proc that executes some dynamic sql. Here's the example
Has anyone seen this error when working with a PHP application out of dynamic
I am working on rendering a dynamic form in an ASP.NET MVC view that
I'm currently working with a part of my application that uses Dynamic Web User
i currently have highcharts realtime graph working on a browser (e.g. http://www.highcharts.com/demo/dynamic-update ), but
I'm working on a site that generates a dynamic image based on data from
The app I am working on requires a dynamic layout that displays a HorizontalScrollView

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.