note 74001 deleted from function.is-numeric by felipe
| From: | felipe@php.net | Date: | Mon, 28 Apr 2008 11:17:42 +0000 |
| Subject: | note 74001 deleted from function.is-numeric by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-138862@lists.php.net to get a copy of this message | ||
Note Submitter: www.kigoobe.com
----
If you are looking for a way to check if the user input is a number, either positive or negative, or
a decimal value (but not with an e, as in is_numeric), the following can be used -
<?php
mynumber = "3" // 3 is a number
$mynumber = "3.3" // 3.3 is a number
$mynumber = "-3.3" // -3.3 is a number
$mynumber = "-0,3" // -0,3 is a number
$mynumber = "-0,3x" // -0,3x is not a number
if (preg_match ("/^([0-9.,-]+)$/", $mynumber)) {
print $mynumber." is a number";
} else {
print $mynumber." is not a number";
}
?>
I have added comma (,) as in many european number system, decimal is expressed with a comma, and not
with a dot. Like, $100 is expressed as $100,00 instead of $100.00
if you don't need that, remove the (,) and, you can add any additional value that you need to
add after the series 0-9.,-