有没有一种简单的方法可以通过短代码使用 wordpress 将数字转换为单词? [英] Is there an easy way to convert a number to a word with wordpress via shortcode?

查看:22
本文介绍了有没有一种简单的方法可以通过短代码使用 wordpress 将数字转换为单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个函数可以用短代码用单词表达任何给定的数字?我已经测试了这个功能,但我认为我的短代码有问题.

Is there a function that will express any given number in words with shortcode ? i have tested this function but i think i have a problem with the shortecode.

我的功能:

function number_to_word( $num = '' ){
$num    = ( string ) ( ( int ) $num );

if( ( int ) ( $num ) && ctype_digit( $num ) )
{
    $words  = array( );

    $num    = str_replace( array( ',' , ' ' ) , '' , trim( $num ) );

    $list1  = array('','one','two','three','four','five','six','seven',
        'eight','nine','ten','eleven','twelve','thirteen','fourteen',
        'fifteen','sixteen','seventeen','eighteen','nineteen');

    $list2  = array('','ten','twenty','thirty','forty','fifty','sixty',
        'seventy','eighty','ninety','hundred');

    $list3  = array('','thousand','million','billion','trillion',
        'quadrillion','quintillion','sextillion','septillion',
        'octillion','nonillion','decillion','undecillion',
        'duodecillion','tredecillion','quattuordecillion',
        'quindecillion','sexdecillion','septendecillion',
        'octodecillion','novemdecillion','vigintillion');

    $num_length = strlen( $num );
    $levels = ( int ) ( ( $num_length + 2 ) / 3 );
    $max_length = $levels * 3;
    $num    = substr( '00'.$num , -$max_length );
    $num_levels = str_split( $num , 3 );

    foreach( $num_levels as $num_part )
    {
        $levels--;
        $hundreds   = ( int ) ( $num_part / 100 );
        $hundreds   = ( $hundreds ? ' ' . $list1[$hundreds] . ' Hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '' );
        $tens       = ( int ) ( $num_part % 100 );
        $singles    = '';

        if( $tens < 20 )
        {
            $tens   = ( $tens ? ' ' . $list1[$tens] . ' ' : '' );
        }
        else
        {
            $tens   = ( int ) ( $tens / 10 );
            $tens   = ' ' . $list2[$tens] . ' ';
            $singles    = ( int ) ( $num_part % 10 );
            $singles    = ' ' . $list1[$singles] . ' ';
        }
        $words[]    = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_part ) ) ? ' ' . $list3[$levels] . ' ' : '' );
    }

    $commas = count( $words );

    if( $commas > 1 )
    {
        $commas = $commas - 1;
    }

    $words  = implode( ', ' , $words );

    //Some Finishing Touch
    //Replacing multiples of spaces with one space
    $words  = trim( str_replace( ' ,' , ',' , trim_all( ucwords( $words ) ) ) , ', ' );
    if( $commas )
    {
        $words  = str_replace_last( ',' , ' and' , $words );
    }

    return $words;
}
else if( ! ( ( int ) $num ) )
{
    return 'Zero';
}
return '';}

add_shortcode( 'convertNumber', 'number_to_word' );

例如:

如果短代码是 [convertNumber num='1432'] ,那么这个函数将在我的 wordpress 页面中返回一千四百三十二".

If a shortcode is [convertNumber num='1432'] , then this function will return "One thousand four hundred and thirty two" in my wordpress Page .

推荐答案

使用 Number Formatter Class 类,方便它.

$number_formatter = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $number_formatter->format(1432);

在WordPress中使用它的示例

Exmaple to use it in WordPress

function number_to_word($atts, $content){
    include('class.numberformatter.php');
    // let's fetch all of the arguments of the shortcode
    $atts = shortcode_atts( 
        array(
            'number' => '0',
        ), $atts );
    $number = $atts['number'];
    $number_formatter = new NumberFormatter("en", NumberFormatter::SPELLOUT);
    $converted = $number_formatter->format($number);
    return $converted;
}
add_shortcode( 'convertNumber', 'number_to_word' );

您可以通过 [convertNumber number="2222"]

谢谢

这篇关于有没有一种简单的方法可以通过短代码使用 wordpress 将数字转换为单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆