PHP: Get Version of ICU

馋奶兔 提交于 2020-03-03 07:34:25

问题


My production site just provides the old ICU version 4.2.1. Since Yii2 requires Version 49.1 or higher I need to make workarounds in PHP.

How do I get the nersion number of ICU (libicu) which is used by PHP during runtime. Since I have frequent production updates I need to get the version number dynamically in PHP code, e.g. by

$libIcuVersion = ...

The version number is shown in phpinfo.php but the output cannot be used in my code.


回答1:


This is how Symfony/Intl does it in Symfony\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:


You can use this slightly modified method that Yii 2 is using:

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;
}



回答3:


I would get and parse phpinfo() results.

<?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

*/


来源:https://stackoverflow.com/questions/44472209/php-get-version-of-icu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!