Results 1 to 8 of 8
  1. #1
    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.

  2. #2
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    1,097
    PHP Code:
    function is_lowercasealpha($string)
    {
       return !
    preg_match("/[^a-z]/"$string);


  3. #3
    Join Date
    Nov 2001
    Location
    California
    Posts
    1,991
    Code:
    						
    eregi('^[a-zA-Z]{10}', $_REQUEST['VAR'])
    That will allow the user to input anything between a-z and A-Z up to 10 letters. The user cannot input a number or 11+ letters. Reg. expressions are probably the best way to do what you want to do

  4. #4
    Join Date
    Nov 2000
    Location
    USA
    Posts
    1,269

    Thumbs up

    Thanks guys, that did the trick.

  5. #5
    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);}

  6. #6
    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

  7. #7
    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.
    Regular Expressions are more versatile though, don't you think?

  8. #8
    Join Date
    May 2004
    Location
    Singapore
    Posts
    263
    Regular Expressions are more versatile though, don't you think?
    Of course, hence one should use them as necessary.
    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);}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •