dongzine3782 2015-12-13 11:46 采纳率: 0%
浏览 99
已采纳

php正则表达式搜索数组键并返回值

I need a regex to search in an array and then return the value of that key.

for example: I want to get the value of the var1 key.

my config

<?php

return [
    'var1' => 'test1',
    'var2' => 'test2',
    'var3' => 'test3'
];

?>

then it should return test1

  • 写回答

1条回答 默认 最新

  • douyun6399 2015-12-13 12:22
    关注

    This is an unorthodox question, but there's a straightforward enough answer here that does not require a regex, so I will present this instead.

    Assume that your config file is called config.php and contains the code snippet that you provided in your example:

    <?php
    
    return [
        'var1' => 'test1',
        'var2' => 'test2',
        'var3' => 'test3',
    ];
    

    You can actually assign the return value of include or require to a variable. For example, in another script (assuming you are in the same directory) you can do this:

    <?php
    // your config file...
    $file = __DIR__ . '/config.php';
    
    // ensure the file exists and is readable
    if (!is_file($file) || !is_readable($file)) {
        throw new RuntimeException(
            sprintf('File %s does not exist or is not readable!', $file)
        );
    }
    
    // include file and assign to variable `$config`
    // which now contains the returned array
    $config = include $file;
    
    echo 'original config:' . PHP_EOL;
    print_r($config);
    
    // update config key `var1`
    $config['var1'] = 'UPDATED!';
    
    echo 'updated config:' . PHP_EOL;
    print_r($config);
    

    This yields:

    original config:
    Array
    (
        [var1] => test1
        [var2] => test2
        [var3] => test3
    )
    updated config:
    Array
    (
        [var1] => UPDATED!
        [var2] => test2
        [var3] => test3
    )
    

    I was originally a little surprised that you could use return outside of the context of a function/method, but it's perfectly valid. You learn something new every day... This use case is actually documented under the documentation for include - refer to Example #5 include and the return statement for more details.

    Please note, that if you are using include or require to pull in untrusted or external scripts, the usual security considerations apply. This is discussed in the documentation linked above.

    Also, if your included file contains a syntax error, then you will probably get a parse error or similar, but I guess that's an obvious point!

    Edit

    Finally, I should point out that your question does not ask how to save the updated configuration back into the file, but you did leave a comment underneath that suggests that you want to do this also.

    However, if you want to update and persist a configuration on file, I would definitely using a more malleable method to write/read this data to/from disk - perhaps json_encode() and json_decode, serialize() and unserialize().

    Nevertheless, here's a naive solution in which you can write your updated config:

    // ...
    
    // file has to be writeable!
    if (!is_writeable($file)) {
        throw new RuntimeException(
            sprintf('File %s is not writeable!', $file)
        );
    }
    
    file_put_contents($file, 
        sprintf('<?php return %s;', var_export($config, true)));
    

    Further reading:

    Hope this helps :)

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部