Re: Validating input

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



--- Ed Curtis <ed@xxxxxxxxxxxxx> 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/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux