douluyezhen4512 2015-06-05 10:15
浏览 122
已采纳

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.

  • 写回答

2条回答 默认 最新

  • doukuipai8544 2015-06-05 10:43
    关注
    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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?