SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Help with Regex

  1. #1
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Regex

    Can someone help me with this regular expression?

    Code:
    if(!preg_match("^[A-Za-z0-9]+$^", "$username"))
    It seems to be blocking some names without spaces and special characters. Names constisting of just letters and numbers.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,173
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    regex

    I don't know that using a caret ^ is the best choice for a delimiter, but are you trim()ing the $username ?

  3. #3
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure I am trimming. What delimiter should I use?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Archbob View Post
    I'm pretty sure I am trimming. What delimiter should I use?
    Forward slash and pound are pretty common delimiters since they don't appear in most expressions. The carat is confusing since that's also the start-of-line indicator and a negation operator within the regex. Since you don't have a carat other than the delimiter at the front of your pattern, usernames that don't meet your criteria will actually pass that test.

    Do you have an example of the ones making it through that you don't want to?

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2007
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you only want letters and numbers then do this it is quicker:
    PHP Code:
    if(!ctype_alnum($username)) 
    Kayzio - We don't hesitate, we accelerate.

  6. #6
    Level 8 Chinese guy Archbob's Avatar
    Join Date
    Sep 2001
    Location
    Somewhere in this vast universe
    Posts
    3,741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks


Bookmarks

Posting Permissions

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