note 10289 added to String functions

From: Date: Tue, 26 Dec 2000 01:20:51 +0000
Subject: note 10289 added to String functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-9071@lists.php.net to get a copy of this message
I wanted to convert numbers to an adjective form (so from "1" to "1st", from "352" to "352nd", etc.), but couldn't find it in PHP, so I wrote this little code to do that for me. Hope you find it useful: function nth($in) { if (!is_long($in)) return(-1); switch ($in) { case 0 : return("0th"); case 1 : return("1st"); case 2 : return("2nd"); case 3 : return("3rd"); case 4 : case 5 : case 6 : case 7 : case 8 : case 9 : case 10 : case 11 : case 12 : case 13 : case 14 : case 15 : case 16 : case 17 : case 18 : case 19 : return($in."th"); } switch (substr($in, -1, 1)) { case 0 : return($in."th"); case 1 : return($in."st"); case 2 : return($in."nd"); case 3 : return($in."rd"); case 4 : case 5 : case 6 : case 7 : case 8 : case 9 : return($in."th"); default : return(0); } } It will return -1 when the input is not an integer and 0 when it couldn't find a match (although that shouldn't happen). http://www.php.net/manual/ref.strings.php

« previous php.notes (#9071) next »