preg_replace()不起作用

I am currently trying to change a variable in a configuration file. I've tried the following:

public static function change(){
    $fl = file("../config.inc.php");
    $key = $_POST['key'];
    $id = $_POST['id'];
    $secret = $_POST['secret'];
    $content = "";
    foreach($fl as $line){
        $content .= $line;
    }
    $content = preg_replace("\$licence = array\(\'key\'=>\'(.*?)\'\);$", "licence = array('key'=>'$key');", $content);

    $content = preg_replace("/\$rey_connect = array\((.*?\'client_id\'=>\')(.*?)('.*?\'client_secret\'=>\')(.*?)(\')(.*?)(?:\));(?:
|$)/", "\$rey_connect = array(\1$id\3$secret\5);", $content);

    $myfile = fopen("../config.inc.php", "w") or die("Unable to open file!");
    $txt = "$content";
    fwrite($myfile, $txt);
    fclose($myfile);
}

on the following string:

<?php
# Bitte bearbeiten Sie diese Datei nicht. #

$mysql = array(  'host'=>'localhost',
                        'database'=>'schnnet',
                        'password'=>'root',
                        'user'=>'root');
$licence = array('key'=>'jZf5hhRd5vqmwTkMB9eq');
$rey_connect = array('active'=>true,'client_id'=>'123','client_secret'=>'123456');
?>

So the regex works perfectly on phpliveregex, but not in my script. Somehow it doesn't affect the configuration file's content.

  1. Do you put your changes back to config?
// this is really BAD approach
$content = file_get_contents("config");
$content = preg_replace(...);
file_put_contents("config", $content);
// or eval($content);
  1. if you want to initiate reconnect to the database with slightly different parameter you can simply move your piece of code to separate function and call it with different parameter, e.g. you have script called utils.php
function connect($host, $db, $pwd, $user, $key, $id, $secret)
{
    $mysql = array('host'=>$host,
                   'database'=>$db,
                   'password'=>$pwd,
                   'user'=>$user);
    $licence = array('key'=>$key);
    $rey_connect = array('active'=>true,'client_id'=>$id,'client_secret'=>$secret);
}

And then include utils.php into another script and call your function there.

include_once 'utils.php';

connect(
 'localhost', 
 'schnnet', 
 'root, 
 'root', 
 'jZf5hhRd5vqmwTkMB9eq', 
 '123', 
 '123456');

UPDATE :

I was able to reproduce issue that you described but have just ran simplified version of your code on my local server and it seems to be working as expected now. Try it.

<?php
    function change()
    {
        $id = 'someId';
        $key = 'someKey';
        $secret = 'someSecret';
        $content = file_get_contents("config.php");

        $content = preg_replace("/(licence[^>]+)([^)]+)(.+)/si", "$1>'" . $key . "'$3", $content);
        $content = preg_replace("/(client_id[^>]+)([^,]+)(.+)/si", "$1>'" . $id . "'$3", $content);
        $content = preg_replace("/(client_secret[^>]+)([^)]+)(.+)/si", "$1>'" . $secret . "'$3", $content);

        file_put_contents("config.php", $content);
    }

    change();
?>

Your code runs fine: http://ideone.com/Gev03z

(Though you don't need the '\' before your replacement string.)

The problem is that you are not writing $content back to the file.

Changing $content only changes your local copy of what was in the file.

EDIT:

This is clearly not a regex issue, but a file permissions issue.

When you call fwrite you do not check if the return is false. Which in this case I expect it is, meaning you cannot write to the file.

Now unless I miss my guess, it is because the file is already held open because $fl. Try adding a close($fl); prior to $myfile.