Quick PHP/RegEx question

Discussion in 'Mac Programming' started by yg17, Nov 16, 2006.

  1. yg17 macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #1
    I suck at Regular Expressions. I've got a string that can only have letters in it, so all I need is a little function to return true if if only has letters or false if there's a number in it. Thanks
     
  2. Zortrium macrumors 6502

    Joined:
    Jun 23, 2003
    #2
    function checkstring( $stringvar ){
    if( preg_match( "/\d/", $stringvar ) )
    return false;
    return true;
    }

    If you want to check for ANY non-word characters (ie, punctuation and such), use \W instead of \d (which only checks for digits).
     
  3. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #3
    Not quite.

    This should work.

    PHP:
    function checkstring$stringvar ){
      return ! 
    preg_match"/[^a-zA-Z]/"$stringvar )
    }
     
  4. Zortrium macrumors 6502

    Joined:
    Jun 23, 2003
    #4
    How is this any different from what I wrote?
     
  5. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #5
    First, any combination of letters and punctuation (eg. "test!_()") would return true in your code example since it passes your test of containing digits, but isn't all letters like the OP wanted

    If you use \W instead like you suggested following your example, then "test_1234" would also return true since the word character set is comprised of letters, numbers, and underscore ([a-zA-Z_0-9]) - also not what the OP wanted

    In my example, letters and ONLY letters are allowed. So cases such as "test1234" or "test()!!" would indeed return false.

    Here is a good site to test PHP regular expressions : http://www.solmetra.com/scripts/regex/
     
  6. yg17 thread starter macrumors G5

    yg17

    Joined:
    Aug 1, 2004
    Location:
    St. Louis, MO
    #6
    Thanks, that works. However, I forgot to mention that it should allow spaces, how would I modify that to accept spaces as well?
     
  7. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #7
    if its just spaces use

    Code:
    [^a-zA-Z ]
    or if any whitespace (space, tab, etc)

    Code:
    [^a-zA-Z\s]
     
  8. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #8
    It's worth mentioning that regex varies considerably from one engine to another. For instance, PERL regex is very different from the standard grep regex. Java has its own pecularities, and I imagine that PHP does as well.

    One of the most common sources of irregularity is what constitutes a special character class like \w. I rarely use them because of that..I'd rather have a portable understanding in my mind that won't break when I move to another platform. So I tend to create character classes explicitly, as kingjr did in his post.
     

Share This Page