note 86796 modified in function.is-numeric by danbrown
| From: | danbrown@php.net | Date: | Tue, 04 Nov 2008 14:26:30 +0000 |
| Subject: | note 86796 modified in function.is-numeric by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146119@lists.php.net to get a copy of this message | ||
Using SpT_King James example, i've re-wrote to cater for digits starting with '+',
commas ',' or period '.'
<?php
function is_numeric_regex($str)
{
$str = "{$str}";
if (in_array($str[0], array('-', '+'))) $str = "{$str[0]}0" .
substr($str, 1);
else $str = "0{$str}";
$eng = preg_match ("/^[+,-]{0,1}([0-9]+)(,[0-9][0-9][0-9])*([.][0-9]){0,1}([0-9]*)$/"
, $str) == 1;
$world = preg_match
("/^[+,-]{0,1}([0-9]+)(.[0-9][0-9][0-9])*([,][0-9]){0,1}([0-9]*)$/" , $str) == 1;
return ($eng or $world);
}
?>
--was--
Using SpT_King James example, i've re-wrote to cater for digits starting with '+',
commas ',' or period '.'
function is_numeric_regex($str)
{
$str = "{$str}";
if (in_array($str[0], array('-', '+'))) $str = "{$str[0]}0" .
substr($str, 1);
else $str = "0{$str}";
$eng = preg_match ("/^[+,-]{0,1}([0-9]+)(,[0-9][0-9][0-9])*([.][0-9]){0,1}([0-9]*)$/"
, $str) == 1;
$world = preg_match
("/^[+,-]{0,1}([0-9]+)(.[0-9][0-9][0-9])*([,][0-9]){0,1}([0-9]*)$/" , $str) == 1;
return ($eng or $world);
}
http://php.net/manual/en/function.is-numeric.php