PHP Snippets
letterfy
Information
(PHP Version >= 4)Functions used in this snippet: is_numeric, strlen, in_array, substr.
Description
str letterfy( int int )
Returns a [typestr[/type] suffix of either st, nd, rd, or th based on the last 1 or two digits of the supplied int
Snippet
<?php
function letterfy($int){
if(!is_numeric($int{strlen($int) - 1})) return;
if(in_array(substr($int,-2), array(11,12,13))) return 'th';
switch($int{strlen($int) - 1}){
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
}
?>
Example
letterfy Can be used in the following way:
<?php
$x = 10;
echo $x . letterfy($x); // 10th
$x = 1;
echo $x . letterfy($x) // 1st
$x = 22;
echo $x . letterfy($x) // 22nd
$x = 33;
echo $x . letterfy($x) // 33rd
$x = 111;
echo $x . letterfy($x) // 111th
?>
0 Responses to letterfy