Created
September 13, 2010 06:02
-
-
Save 0xnbk/576871 to your computer and use it in GitHub Desktop.
Add (th, st, nd, rd, th) to the end of a number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function make_ranked($rank) { | |
| $last = substr( $rank, -1 ); | |
| $seclast = substr( $rank, -2, -1 ); | |
| if( $last > 3 || $last == 0 ) $ext = 'th'; | |
| else if( $last == 3 ) $ext = 'rd'; | |
| else if( $last == 2 ) $ext = 'nd'; | |
| else $ext = 'st'; | |
| if( $last == 1 && $seclast == 1) $ext = 'th'; | |
| if( $last == 2 && $seclast == 1) $ext = 'th'; | |
| if( $last == 3 && $seclast == 1) $ext = 'th'; | |
| return $rank.$ext; | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add (th, st, nd, rd, th) to the end of a number
Another useful snippet which will automatically add st, nd, rd or th after a number.