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条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式