note 79819 added to function.is-numeric

From: Date: Thu, 13 Dec 2007 21:11:42 +0000
Subject: note 79819 added to function.is-numeric
Groups: php.notes 
Request: Send a blank email to php-notes+get-133434@lists.php.net to get a copy of this message
is_numeric() says "1/4" is not numeric so I made something that would say yes in this case. <?php function is_number_like ( $val ) { $val = preg_split( '#[ |/]#', $val ); $ret = true; foreach ( $val as $num ) { if ( !is_numeric( $num ) ) { $ret = false; break; } } return $ret; } //test this function - strings, integers, fractions, weird fractions, etc $numbers = array( '1', '1.12', 1.123, '1 1/2', '1/4', '0/0', 'not a number' ); foreach ( $numbers as $num ) { if ( is_number_like( $num ) ) { echo "'$num' is a number\n"; }else { echo "'$num' is NOT a number\n"; } } echo "\n"; ?> This outputs: '1' is a number '1.12' is a number '1.123' is a number '1 1/2' is a number '1/4' is a number '0/0' is a number 'not a number' is NOT a number You'll notice that 0/0 is still recognized as a number although it's technically NOT a number, this is trivial to fix if you need this capability... I didn't... All I needed to know was whether it looked like a number =) ---- Server IP: 66.207.199.35 Probable Submitter: 24.68.129.82 ---- Manual Page -- http://www.php.net/manual/en/function.is-numeric.php Edit -- https://master.php.net/note/edit/79819 Del: integrated -- https://master.php.net/note/delete/79819/integrated Del: useless -- https://master.php.net/note/delete/79819/useless Del: bad code -- https://master.php.net/note/delete/79819/bad+code Del: spam -- https://master.php.net/note/delete/79819/spam Del: non-english -- https://master.php.net/note/delete/79819/non-english Del: in docs -- https://master.php.net/note/delete/79819/in+docs Del: other reasons-- https://master.php.net/note/delete/79819 Reject -- https://master.php.net/note/reject/79819 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#133434) next »