note 99876 added to function.is-numeric
| From: | Nitrogen at osu1 dot php dot net | Date: | Sun, 12 Sep 2010 05:40:09 +0000 |
| Subject: | note 99876 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172491@lists.php.net to get a copy of this message | ||
I find it a little weird that people are having issues with ordinal numbers, it's pretty easy..
Notes are in the commenting, check out the example outputs.
<?php
function ordinal($i='') {
// a temporary value we can change, and keep the original value.
$o=$i;
// suffixes = 0th, 1st, 2nd, third == zeroth, first, second, third
$s=array('th','st','nd','rd');
// if input just so happens to be a string, we check to make sure it
// still holds a numeric value and only acquire the last 2 numbers.
// if it's not a string, nor an integer, we freak out and say no.
if(!is_int($o))
if(ctype_digit($o))
$o=(int)substr($o,-2,2);
else
return(false);
// basically, if $o is between 11 and 19, we use 'th'
// otherwise we use the last digit and if it's over
// 4 then we use 0 (for the $s array index).
return($i.$s[($o%100>10&&$o%100<20)?0:($o%10<4?$o%10:0)]);
}
var_dump(ordinal(5));
/* Example Outputs of ordinal():
0th, 1st, 2nd, 3rd, 4th, ..., 9th,
10th, 11th, 12th, 13th, 14th, ..., 19th,
110th, 111th, ..., 199th, 200th, 201st.
ordinal('-1'); returns false because ctype_digit hates anything that
isn't strictly 0 through 9 and '-' trips it to false.
ordinal('asdf'); returns false for the exact same reason.
ordinal(); returns false because it's blank.
signed integers on a 32-bit system (and the same issue on a 64-bit
system using 0x7FFFFFFFFFFFFFFF because of two's compliment,
anything higher will become a negative number):
ordinal(0x7FFFFFFF ); returns 2147483647th (which is correct)
ordinal(0x7FFFFFFF+1); returns false.
*/
?>
----
Server IP: 92.48.74.199
Probable Submitter: 86.142.216.151
----
Manual Page -- http://www.php.net/manual/en/function.is-numeric.php
Edit -- https://master.php.net/note/edit/99876
Del: integrated -- https://master.php.net/note/delete/99876/integrated
Del: useless -- https://master.php.net/note/delete/99876/useless
Del: bad code -- https://master.php.net/note/delete/99876/bad+code
Del: spam -- https://master.php.net/note/delete/99876/spam
Del: non-english -- https://master.php.net/note/delete/99876/non-english
Del: in docs -- https://master.php.net/note/delete/99876/in+docs
Del: other reasons-- https://master.php.net/note/delete/99876
Reject -- https://master.php.net/note/reject/99876
Search -- https://master.php.net/manage/user-notes.php