SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate string using regex

    Hi Guys!

    I have the following regex, but I also want to allow the following characters:

    ?
    *
    !
    :
    "
    '

    PHP Code:
    if(!preg_match('/^[a-z0-9 \(\)\&,_\/-]{1,100}$/i'$string)) {
                return 
    false;
            } else {
                return 
    true;
            } 
    How do I modify the code?

    Thanks

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    PHP Code:
    return (bool)preg_match('~^[a-z0-9 \(\)&,_/\?\*!:"\'-]{1,100}$~i'$string); 
    Code:
    a-z - match a through z and A through Z (because if the i modifier)
    0-9 - match 0 through 9
    \(  - match ( literally -- needs to be escaped because ( has a special meaning in regex
    \)  - match ( literally -- needs to be escaped because ) has a special meaning in regex
    &   - match & literally
    ,   - match , literally
    _   - match _ literally
    /   - match / literally
    \?  - match ? literally -- needs to be escaped because ? has a special meaning in regex
    \*  - match * literally -- needs to be escaped because ? has a special meaning in regex
    !   - match ! literally
    :   - match : literally
    "   - match " literally
    \'  - match ' literally - escaped here because the string is delimited with single quotes and the string would end here if we didn't escape it
    -   - match - literally - should be escaped in a character set, but since it's at the end of the character set you don't necessarily have to
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

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

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    PHP Code:
    return (bool)preg_match('~^[a-z0-9 \(\)&,_/\?\*!:"\'-]{1,100}$~i'$string); 
    Code:
    a-z - match a through z and A through Z (because if the i modifier)
    0-9 - match 0 through 9
    \(  - match ( literally -- needs to be escaped because ( has a special meaning in regex
    \)  - match ( literally -- needs to be escaped because ) has a special meaning in regex
    &   - match & literally
    ,   - match , literally
    _   - match _ literally
    /   - match / literally
    \?  - match ? literally -- needs to be escaped because ? has a special meaning in regex
    \*  - match * literally -- needs to be escaped because ? has a special meaning in regex
    !   - match ! literally
    :   - match : literally
    "   - match " literally
    \'  - match ' literally - escaped here because the string is delimited with single quotes and the string would end here if we didn't escape it
    -   - match - literally - should be escaped in a character set, but since it's at the end of the character set you don't necessarily have to
    Perfect, thanks! :-)

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Code:
    \(  - match ( literally -- needs to be escaped because ( has a special meaning in regex
    \)  - match ( literally -- needs to be escaped because ) has a special meaning in regex
    \?  - match ? literally -- needs to be escaped because ? has a special meaning in regex
    \*  - match * literally -- needs to be escaped because ? [sic] has a special meaning in regex
    None of these comments are true, within a character class.

    Quote Originally Posted by ScallioXTX View Post
    Code:
    -   - match - literally - should be escaped in a character set, but since it's at the end of the character set you don't necessarily have to
    It should be escaped when in a position that would otherwise denote a character range. This means that it's okay to not escape at the start/end of the character class and immediately after a character range.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Salathe View Post
    None of these comments are true, within a character class.
    I had the feeling I was missing something ...

    Quote Originally Posted by Salathe View Post
    It should be escaped when in a position that would otherwise denote a character range. This means that it's okay to not escape at the start/end of the character class and immediately after a character range.
    Indeed!
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    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
  •