« Migration from Vox || Simple 5 Star Rating System PHP/MySQL »

Adding Appropriate Suffix to Numerical Value Via PHP

1


Comes in handy.

 
<? function addsuffix($number) {
 
if (!is_numeric($number)) return $number;
$number = floor(abs($number));
$suffixarray = array("th","st","nd","rd");
$numbersuffix = $suffixarray[0];
if (isset($suffixarray[substr($number,-1)]) (strlen($number) < 2 || substr($number,-2,1) != 1)) $numbersuffix = $suffixarray[substr($a,-1)];
return $number.$numbersuffix;
}
?>
 

Converts 1 to 1st, 2 to 2nd, 1837592348739832 to 1837592348739832nd, and so on.


This entry was posted on Monday, March 3rd, 2008 at 3:36 pm and is filed under Blog, PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Get a Trackback link

1 Trackbacks/Pingbacks

  1. Pingback: links for 2008-03-11 at Kevin Smith on March 11, 2008

Leave a comment