PHP:使用字词(例如,第一,第二,第三等)的递增计数器功能 [英] PHP: increment counter function using words (i.e. First, Second, Third, etc.. )

查看:75
本文介绍了PHP:使用字词(例如,第一,第二,第三等)的递增计数器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找到一种可以通过单词增加计数器的功能。我知道使用带后缀数字(例如1st,2nd,3rd等)的可能性。这是我得到的代码的一个片段:

I've been trying to find a function which increments a counter using words. I know its possible using numbers with suffixes (i.e. 1st, 2nd, 3rd and so on). Here is a snippet of the code i've got:

function addOrdinalNumberSuffix($num) {
    if (!in_array(($num % 100),array(11,12,13))){
        switch ($num % 10) {
            // Handle 1st, 2nd, 3rd
            case 1:  return $num.'st';
            case 2:  return $num.'nd';
            case 3:  return $num.'rd';
        }
    }
    return $num.'th';
}

代码源

但是有没有办法用单词(例如,第一,第二,第三等)来复制呢?

But is there a way to replicate this with words (i.e First, Second, Third, etc..)?

I我希望创建一个无限计数器非常困难(但并非不可能),但是最多20个就足够了。

I'd expect it to be quite difficult (but not impossible) to create an infinite counter, but anything up to 20 would suffice.

任何帮助将不胜感激。 / p>

Any help would be much appreciated.

推荐答案

有一个 class 可以做到:

<?php

// include class
include("Numbers/Words.php");

// create object
$nw = new Numbers_Words();

// convert to string
echo "600 in words is " . $nw->toWords(600);

?>

来源

这篇关于PHP:使用字词(例如,第一,第二,第三等)的递增计数器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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