0
$test =array('APP_TITLE' =>'ssfss');
update_define($test);

function update_define($arr_con){
    $file='config.php';
    $fileContent= file_get_contents($file);
    $i = 0;
    $define_arr = array();
    $new_value_arr = array();

    foreach ($arr_con as $constant => $vale){
        $line = getLine($constant);

        $define_arr[$i] = $line;
        if(($vale == 'true') || ($vale == 'false')){
            $new_value_arr[$i] = "define('$constant', $vale)";
        }else{
            $new_value_arr[$i] = "define('$constant', '$vale')";
        }
        $i++;
    }
    $modified=preg_replace($define_arr, $new_value_arr, $fileContent);

    file_put_contents($file, $modified);

}

function getLine($string){
    $ret = '';
    $file = fopen("config.php", "r") or exit("Unable to open file!");
    //Output a line of the file until the end is reached
    $i = 0;
    while(!feof($file))
    {
        $i++;
        $line = fgets($file);
        $pos = strpos($line, $string);
        if( $pos !== false ){
            $test =array('APP_TITLE' =>'ssfss');
            update_define($test);

function update_define($arr_con){
    $file='config.php';
    $fileContent= file_get_contents($file);
    $i = 0;
    $define_arr = array();
    $new_value_arr = array();

            foreach ($arr_con as $constant => $vale){
                $line = getLine($constant);

                $define_arr[$i] = $line;
                if(($vale == 'true') || ($vale == 'false')){
                    $new_value_arr[$i] = "define('$constant', $vale)";
                }else{
                    $new_value_arr[$i] = "define('$constant', '$vale')";
                }
                $i++;
                }
                $modified=preg_replace($define_arr, $new_value_arr, $fileContent);

                file_put_contents($file, $modified);

            }

        function getLine($string)
        {

        $ret = '';
            $file = fopen("config.php", "r") or exit("Unable to open file!");
            //Output a line of the file until the end is reached
            $i = 0;
            while(!feof($file))
            {
                $i++;
                $line = fgets($file);
                $pos = strpos($line, $string);
                if( $pos !== false ){
                    //          echo $i.'<br/>';
                    $line = str_replace("define(", "define\(",$line);
                    $line = str_replace(");","\);", $line);
                    $ret= $line;
                }
            }
            fclose($file);
            return $ret;
        }
                $line = str_replace("define(", "define\(",$line);
                    $line = str_replace(");","\);", $line);
                    $ret= $line;
                }
            }
            fclose($file);
            return $ret;
        }

config.php 文件

<?PHP
    define ( 'APP_TITLE', 'test com title' ); 
    define('COMPANY_ADDRESS_2', '49 sd d');
    define('COMPANY_ZIPCODE', '2085');
    define('COMPANY_PHONE', '+44 (0)2 8007-5554');
    define('COMPANY_FAX', '+62 (0)2 253-9479');
?>

上面的代码给了我下面的错误

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /opt/lampp/htdocs/Edit_file/abc.php on line 46
Array ( [0] => define\('APP_TITLE', 'test com title' \); ) /nArray ( [0] => define('APP_TITLE', 'ssfss') ) 

我试图使用上面的方法编辑一些 php 文件,但调用 preg_replace() 方法给了我一些错误。请帮助解决这个问题

4

1 回答 1

3

使用preg_replace模式时应该分隔,以便解析器知道模式的开始和结束位置。经常使用的分隔符是正斜杠/

$a = preg_replace(array("/1/", "/3/"), array("10", "30"), "this is a 1 2 3 test");

$a现在成立this is a 10 2 30 test

您需要做的是用分隔符将您的模式括起来:

$define_arr = Array ( [0] => "/define('APP_TITLE', 'some test' )/" );
$new_value_arr = Array ( [0] => "define('APP_TITLE', 'ssfss')" ):


preg_replace($define_arr, $new_value_arr, $fileContent);

那应该行得通。

但请注意,您实际上并不需要数组或preg_replace这种简单的替换。改为使用str_replace

于 2012-10-23T07:25:22.987 回答