Re: Newbie pattern question
| From: | Matthew Weier O'Phinney | Date: | Fri, 05 Nov 2004 19:01:11 +0000 |
| Subject: | Re: Newbie pattern question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-201304@lists.php.net to get a copy of this message | ||
* Phpu <phpu@go.ro>:
> I have this function
>
> function validate_name($name) {
>
> if(ereg("^[a-zA-Z0-9_]{2,30}$", $name)) {
>
> return true;
>
> } else {
>
> return false;
>
> }
>
> }
>
> If i enter a name like John for exemple everything is ok but if i enter =
> John Doe the function return false.
You need to add a space to your character class:
if (ereg("^[a-zA-Z0-9_ ]{2,30}$", $name)) {
return true;
}
However, note also: pattern matching such as this is faster and more
flexible if you use the preg_* functions instead of the ereg_*
functions:
if (preg_match('/^[a-z0-9_ ]{2,30}$/i', $name)) {
return true;
}
will perform faster, and gives you case insensitivity with the 'i'
switch following the regexp.
--
Matthew Weier O'Phinney | mailto:matthew@garden.org
Webmaster and IT Specialist | http://www.garden.org
National Gardening Association | http://www.kidsgardening.com
802-863-5251 x156 | http://nationalgardenmonth.org