SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expressions

    I am trying to learn regular expressions and have a question for the following codes

    Why does this code allow everything
    return (preg_match_all ("/[a-z]\'*\-*\s*/", $testString));

    Why does this allow nothing
    return (preg_match_all ("/^[a-z]\'*\-*\s*$/", $testString));

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    What test strings are you using?

    The difference between the two is quite simple, the latter requires the beginning of the string to match a-z (lowercase) and end with at least 1 space.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then what do I do to make the 1st code only allow whats listed?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,813
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    Tell me what strings you want captured and what you do not want captured and I can answer that.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is for first and last name, I want to allow for spaces, hyphens and apostrophes. Maybe you clarify my assumptions about regular expressions. Does it return 0 when a character is inserted that is not in the argument or does it just check to see if the listed characters are there?

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cpradio View Post
    The difference between the two is quite simple, the latter requires the beginning of the string to match a-z (lowercase) and end with at least 0 spaces.
    There, fixed that for you
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,897
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by tompat View Post
    That is for first and last name, I want to allow for spaces, hyphens and apostrophes.
    I think what you're looking for this

    Code:
    ~^[a-zA-Z'\s-]+$~
    Please note that this does not allow for special characters like the é in my name.
    For names I mostly find it easiest to just strip_tags to prevent script injection and be done with it.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


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
  •