添加'rd或'th或'st依赖于数字 [英] add 'rd or 'th or 'st dependent on number

查看:86
本文介绍了添加'rd或'th或'st依赖于数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在PHP中显示带有序数后缀的数字

Possible Duplicate:
Display numbers with ordinal suffix in PHP

我认为它们被称为序数后缀.

I think they are called Ordinal suffixes.

看过日期示例...

但是只是想知道是否有一些php可以喷出依赖于数字的后缀.

But just wondered if there was some php that can spew out the suffix dependant on number.

示例:我们正在释放用户的排行榜得分.

Example: we are spewing out the leaderboard score of our users.

因此,该会员排名第一.我们要回声:1日 并且该会员排名847.我们想吐出第847位

So member ranked number 1. we wish to echo: 1'st and member ranked 847. we want to spew out 847'th

等等等

我无法举个例子,因为数字是通过dB在页面上呈现的

I cannot give example, as the numbers are rendered on page via our dB

只是想知道是否存在某种代码片段,可以自动将'th或'st或'rd添加到适当的数字.

Just wondered if there was some sort of code snippet, that could add automagically 'th or 'st or 'rd to the appropriate number.

推荐答案

我不知道有任何内置插件可以执行此操作,但这应该可以:

I don't know of any built-ins that do this, but this should work:

function ordinal_suffix($num){
    $num = $num % 100; // protect against large numbers
    if($num < 11 || $num > 13){
         switch($num % 10){
            case 1: return 'st';
            case 2: return 'nd';
            case 3: return 'rd';
        }
    }
    return 'th';
}

(注释11、12和13是特殊情况-第11、12、13和11日...)

(note 11, 12 and 13 are special cases - 11th, 12th, 13th vs 11st...)

这篇关于添加'rd或'th或'st依赖于数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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