Re: [PHP] validating input
blackwater dev wrote: I want to check a string for only numbers and letters but am banging my head with regex: ctype_alnum() Chris -- Chris Shiflett Brain Bulb, The PHP Consultancy http://brainbulb.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] validating input
On 12 Apr 2005 blackwater dev wrote: $good = joh_'; // Let's check the good e-mail if (preg_match(/[a-z0-9]/, $good)) { echo Good; } else { echo Bad; } This returns Good, why? That regex matches any string which contains at least one (lowercase) letter or one number somewhere within it. To match for only those elements and no others you probably want: if (preg_match(/^[a-z0-9]*$/, $good)) { which will match a string which contains nothing but a-z and 0-9, and will also match the empty string. See http://www.php.net/manual/en/reference.pcre.pattern.syntax.php and look at the meanings of ^, $, *, and +. Other variations for the regex: /^[a-z0-9]+$/ as above but will not match empty string /^[a-z0-9]*$/i as above but matches upper case as well /^[a-zA-Z0-9]*$/like previous line -- as above but matches upper case as well As Chris S. pointed out, a regex is not needed for this task, so the above is just academic. -- Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating input
Ed Curtis wrote / napsal (a): 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? Thanks Ed whatabout trim? left or right? trobi -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating input
Ed Curtis 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? Thanks Ed You really don't need a regex for this Can it be a float? is_numeric() http://us4.php.net/is_numeric Only a float? is_float() http://us4.php.net/is_float Only an integer? is_int() http://us4.php.net/is_int -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating input
John Nichel wrote: Ed Curtis 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? Thanks Ed There's also ctype_digit... http://us4.php.net/ctype_digit And if you really want to use a regex to check for just digits... /^\d{1,}$/ -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Validating input
--- Ed Curtis [EMAIL PROTECTED] 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