note 99876 deleted from function.is-numeric by crell

From: Date: Mon, 18 Apr 2022 14:56:16 +0000
Subject: note 99876 deleted from function.is-numeric by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218277@lists.php.net to get a copy of this message
Note Submitter: Nitrogen ---- 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. */ ?>

« previous php.notes (#218277) next »