【问题标题】:PHP: Get Version of ICUPHP:获取 ICU 的版本
【发布时间】:2017-06-10 10:24:24
【问题描述】:

我的生产站点只提供旧的 ICU 版本 4.2.1。由于 Yii2 需要 49.1 或更高版本,我需要在 PHP 中进行变通。

如何获取 PHP 在运行时使用的 ICU (libicu) 的 nersion 编号。由于我经常进行生产更新,我需要在 PHP 代码中动态获取版本号,例如由

$libIcuVersion = ...

版本号显示在phpinfo.php,但输出不能在我的代码中使用。

【问题讨论】:

标签: php yii2 icu intl


【解决方案1】:

这就是Symfony/IntlSymfony\Component\Intl::getIcuVersion() 中的做法。

try {
    $reflector = new \ReflectionExtension('intl');
    ob_start();
    $reflector->info();
    $output = strip_tags(ob_get_clean());
    preg_match('/^ICU version (?:=>)?(.*)$/m', $output, $matches);
    $icuVersion = trim($matches[1]);
} catch (\ReflectionException $e) {
    $icuVersion = null;
}

【讨论】:

    【解决方案2】:

    你可以使用 Yii 2 使用的这个稍微修改过的方法:

    function checkPhpExtensionVersion($extensionName)
    {
        if (!extension_loaded($extensionName)) {
            return false;
        }
        $extensionVersion = phpversion($extensionName);
        if (empty($extensionVersion)) {
            return false;
        }
        if (strncasecmp($extensionVersion, 'PECL-', 5) === 0) {
            $extensionVersion = substr($extensionVersion, 5);
        }
    
        return $extensionVersion;
    }
    

    【讨论】:

    • 这向我展示了一个 php_intl 扩展版本(在我的情况下为 PHP 7.2.1 的 1.1.0),而不是根据 phpinfo() 输出的 ICU 库版本 60.1。
    【解决方案3】:

    我会获取并解析 phpinfo() 结果。

    <?php
    
    // get ICU library version in current PHP
    
    // check environment
    if (php_sapi_name() !== "cli") {
        exit('This only works in PHP command line (unless you write phpinfo()\'s html parser)');
    }
    
    // get phpinfo() std output into buffer
    ob_start();
    phpinfo();
    
    // search all buffer starting with 'ICU'
    preg_match_all(
        '/^(?P<name>ICU(?: [A-Za-z_]*)? version) => (?P<version>.*)$/m',
        ob_get_clean(),
        $matched,
        PREG_SET_ORDER
    );
    if (count($matched) === 0) {
        exit('no ICU library info found in phpinfo(). Your PHP may not have php_intl extension turned on.');
    }
    foreach($matched as $current) {
        echo $current['name'] . ": " . $current['version'] . PHP_EOL;
    }
    
    /* output sample
    
    ICU version:60.1
    ICU Data version:60.1
    ICU TZData version:2017c
    ICU Unicode version:10.0
    
    */
    

    【讨论】:

      【解决方案4】:

      试试这个:

       php -r "echo phpinfo();" |grep -i 'icu'
      

      【讨论】:

        猜你喜欢
        • 2019-05-28
        • 1970-01-01
        • 2017-01-31
        • 2017-01-15
        • 2019-12-04
        • 2021-08-25
        • 2015-09-23
        • 1970-01-01
        相关资源
        最近更新 更多