尝试更改.env文件中的值

时间:2015-09-07 14:33:13

标签: php

我尝试编写一个从.env文件中获取内部版本号的脚本,向其中添加1并将其保存回该文件。我目前有以下代码,但我不知道如何将其保存回文件。有什么帮助吗?

$env = file_get_contents('../app/config/.env');

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

if (strpos($env,'build(') !== false) {
    $parsed = get_string_between($env, "build(", ")");
    $parsed = $parsed++;
}

我的.env文件如下所示:

VERSION_NUMBER="v.1.0 build(32)"
ENVIRONMENT="alpha"

4 个答案:

答案 0 :(得分:0)

试试这个:

$handle = fopen('../app/config/.env', 'w');
$content = "VERSION_NUMBER=\"v.1.0 build($parsed)\" \n ENVIRONMENT=\"alpha\" ";
fwrite($handle, $content);
fclose($handle);

答案 1 :(得分:0)

事实证明,我可以explode()并将它们全部链接在一起。

$env = file_get_contents('../app/config/.env');

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

if (strpos($env,'build(') !== false) {
    $parsed = get_string_between($env, "build(", ")");
    $data = explode($parsed, $env);

    $parsed = intval($parsed);
    $parsed++;
    $result = $data[0].$parsed.$data[1];
    echo $result;
}

答案 2 :(得分:0)

试试这个

$string = 'build(32)';
$pattern = '/build\\(([\\d]{2})\\)/';
echo preg_replace_callback($pattern, 'call', $string);

function call($v)
{
    return 'build('.($v[1]+1).')';
}

答案 3 :(得分:0)

也许我对复杂的事情感到困惑,但你的ENV文件看起来与INI文件完全相同。因此我会写这样的东西(解释在代码注释中):

<?php

# File path
$path = "../app/config/";

# ENV extension
$env = ".env";

# Parse ENV as INI file
$arr = parse_ini_file($path.$env);

# Get current version number
$num = preg_match("#\((.*?)\)#i", $arr['VERSION_NUMBER'], $match);

# Add 1 to version number
$match[1]++;

# Replace version number
$arr['VERSION_NUMBER'] = preg_replace("#\((.*?)\)#", "(".$match[1].")", $arr['VERSION_NUMBER']);

# Function that writes back to ENV file
function write_ini_file($assoc_arr, $path, $has_sections=FALSE) { 
    $content = ""; 
    if ($has_sections) { 
        foreach ($assoc_arr as $key=>$elem) { 
            $content .= "[".$key."]\n"; 
            foreach ($elem as $key2=>$elem2) { 
                if(is_array($elem2)) 
                { 
                    for($i=0;$i<count($elem2);$i++) 
                    { 
                        $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                    } 
                } 
                else if($elem2=="") $content .= $key2." = \n"; 
                else $content .= $key2." = \"".$elem2."\"\n"; 
            } 
        } 
    } 
    else { 
        foreach ($assoc_arr as $key=>$elem) { 
            if(is_array($elem)) 
            { 
                for($i=0;$i<count($elem);$i++) 
                { 
                    $content .= $key."[] = \"".$elem[$i]."\"\n"; 
                } 
            } 
            else if($elem=="") $content .= $key." = \n"; 
            else $content .= $key." = \"".$elem."\"\n"; 
        } 
    } 

    if (!$handle = fopen($path, 'w')) { 
        return false; 
    }

    $success = fwrite($handle, $content);
    fclose($handle); 

    return $success; 
}

# Save data back to ENV file
write_ini_file($arr, $path.$env);

?>