首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP Pear Number_Words

PHP Pear Number_Words
EN

Stack Overflow用户
提问于 2014-11-28 01:41:31
回答 1查看 354关注 0票数 0

)我开始使用PEAR Number_Word类,它工作得很好!它通过这种方式将数字转换为字符串:

代码语言:javascript
复制
<?php
include_once 'Numbers/Words.php';
$number = 100254;
     $nw = new Numbers_Words();
     $numw = $nw->toWords($number);

     print $numw;

?>

但现在我需要同时将相同的数字转换为不同的语言。举个例子:我有100个数字,我需要同时把它转换成英语和法语。我试图在参数中传递两种语言的数组,但这不起作用:

代码语言:javascript
复制
/**
     * Default Locale name
     * @var string
     * @access public
     */
    public $locale = 'en_US'; //__her I was try to pass an array, but no result__

    /**
     * Default decimal mark
     * @var string
     * @access public
     */
    public $decimalPoint = '.';

    // }}}
    // {{{ toWords()

    /**
     * Converts a number to its word representation
     *
     * @param integer $num     An integer between -infinity and infinity inclusive :)
     *                         that should be converted to a words representation
     * @param string  $locale  Language name abbreviation. Optional. Defaults to
     *                         current loaded driver or en_US if any.
     * @param array   $options Specific driver options
     *
     * @access public
     * @author Piotr Klaban <makler@man.torun.pl>
     * @since  PHP 4.2.3
     * @return string  The corresponding word representation
     */
    function toWords($num, $locale = '', $options = array())
    {
        if (empty($locale) && isset($this) && $this instanceof Numbers_Words) {
            $locale = $this->locale;
        }

        if (empty($locale)) {
            $locale = 'en_US';
        }

        $classname = self::loadLocale($locale, '_toWords');


        $obj = new $classname;


        if (!is_int($num)) {
            $num = $obj->normalizeNumber($num);

            // cast (sanitize) to int without losing precision
            $num = preg_replace('/(.*?)('.preg_quote($obj->decimalPoint).'.*?)?$/', '$1', $num);
        }

        if (empty($options)) {
            return trim($obj->_toWords($num));
        }
        return trim($obj->_toWords($num, $options));
    }

如果你知道怎么做,请叫我停一下!提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2015-02-20 22:16:50

Numbers_Words不支持同时支持多种语言。

您需要为每种语言创建一个新的Numbers_Words实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27176370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档