Results 1 to 8 of 8
Thread: PHP Regular Expression Question
-
05-09-2005, 01:21 AM #1Web Hosting Master
- Join Date
- Nov 2000
- Location
- USA
- Posts
- 1,269
PHP Regular Expression Question
Fogive the basic question... this seems like it should be pretty simple but I'm really not sure how to do it.
I want to test whether a string of characters contains anything other than a-z.
For example, "ghegrh" or "kdqoek" would pass. "ehr%thq" or "th85.ehr" would fail.
I would think this could be done with a regular expression but if there's a better way to do it that would be great.
-
05-09-2005, 01:27 AM #2Web Hosting Master
- Join Date
- Dec 2004
- Location
- Canada
- Posts
- 1,097
PHP Code:function is_lowercasealpha($string)
{
return !preg_match("/[^a-z]/", $string);
}
-
05-09-2005, 01:30 AM #3Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
Code:eregi('^[a-zA-Z]{10}', $_REQUEST['VAR'])
-
05-09-2005, 01:41 AM #4Web Hosting Master
- Join Date
- Nov 2000
- Location
- USA
- Posts
- 1,269
Thanks guys, that did the trick.
-
05-09-2005, 02:00 AM #5Web Hosting Guru
- Join Date
- May 2004
- Location
- Singapore
- Posts
- 263
Actually, if you allow for both uppercase and lowercase letters, then use ctype_alpha() in the C locale.
It is faster than regular expressions, which is overkill in this case.#include<cstdio>
char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
int main(){std::printf(s,10,34,s,34,10);}
-
05-09-2005, 02:04 AM #6Web Hosting Master
- Join Date
- Nov 2000
- Location
- USA
- Posts
- 1,269
I've been fooling around with PHP for years and I didn't even know "ctype" existed. It's exactly what I wanted.
Just found this: http://us2.php.net/manual/en/ref.ctype.php
-
05-09-2005, 02:26 AM #7Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
Originally posted by laserlight
Actually, if you allow for both uppercase and lowercase letters, then use ctype_alpha() in the C locale.
It is faster than regular expressions, which is overkill in this case.
-
05-09-2005, 04:27 AM #8Web Hosting Guru
- Join Date
- May 2004
- Location
- Singapore
- Posts
- 263
Regular Expressions are more versatile though, don't you think?
They are not necessary in this case.#include<cstdio>
char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
int main(){std::printf(s,10,34,s,34,10);}