Re: Validating input
| From: | Chris Shiflett | Date: | Fri, 28 Jan 2005 22:16:49 +0000 |
| Subject: | Re: Validating input | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-207640@lists.php.net to get a copy of this message | ||
--- Ed Curtis <ed@homes2see.com> wrote:
> I've been looking at the docs and found preg_match and
> preg_match_all but these functions only seem to match 1
> specific search item. I want to make sure a variable (say
> $mlsnumber) contains only numbers and no spaces. What
> would I use to accomplish this?
You mean only numerics? You can use ctype_digit():
http://php.net/manual/function.ctype-digit.php
I write more about the character type functions here:
http://shiflett.org/archive/84
There is also is_numeric(), if you want to allow any valid representation
of a numeric:
http://php.net/manual/function.is-numeric.php
Be careful with functions like is_int():
http://php.net/manual/function.is-int.php
They check the actual data type. If you're filtering data from the client
(POST, GET, or cookies), then it's going to be a string, even if it looks
like a number.
However, you can use an approach like the following to make sure something
is an integer:
<?php
$clean = array();
if ($_POST['num'] === strval(intval($_POST['num'])))
{
$clean['num'] = $_POST['num'];
}
?>
Hope that helps.
Chris
=====
Chris Shiflett - http://shiflett.org/
PHP Security - O'Reilly HTTP Developer's Handbook - Sams
Coming Soon http://httphandbook.org/