PHP:获取 ICU 版本

问题描述 投票:0回答:5

我的生产站点仅提供旧的 ICU 版本 4.2.1。由于 Yii2 需要版本 49.1 或更高版本,我需要在 PHP 中做出解决方法。

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

$libIcuVersion = ...

版本号显示在

phpinfo.php
中,但输出不能在我的代码中使用。

php yii2 icu intl
5个回答
3
投票

这就是 Symfony/Intl

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
投票

试试这个:

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

1
投票

您可以使用 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;
}

1
投票

我会获取并解析 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

*/

0
投票

我尝试了akky的版本。但这对我不起作用。经过一番努力,我最终得到了以下结果:

ob_start();
phpinfo();
$icudata = ob_get_contents();
ob_end_clean();

preg_match_all(
    '/<tr><td[^>]*>(ICU(?: [A-Za-z_]*)? version)[^<]*<\/td><td[^>]*>([^<]*)/',
    $icudata,
    $matches
);

$count = sizeof($matches[0]);
if ($count == 0) 
  echo 'no ICU library info found in phpinfo(). Your PHP may not have php_intl extension turned on.';
else
{ for($i=0; $i<$count; $i++)
    echo $matches[1][$i] . ": " .trim($matches[2][$i]) . '<br>';
}
© www.soinside.com 2019 - 2024. All rights reserved.