preg_match with brackets in expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Untam3d
    New Member
    • Aug 2009
    • 3

    preg_match with brackets in expression

    Ok, I am using this code to check the form for invalid characters:

    Code:
    if(preg_match('/^[a-zA-Z0-9]$/', $char)){
    REST OF MY CODE HERE
    This is what $char is:

    Code:
    $char = $_POST['charname'];
    And $char connects to a form. My problem is that, I only want a-z, A-Z, 0-9, and [ and ] (aka brackets) allowed.

    So far only a-z, A-Z, and 0-9 are allowed... I am not sure how to add brackets to that. Help please? Thanks!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    escape the brackets with a backslash.

    Comment

    • Untam3d
      New Member
      • Aug 2009
      • 3

      #3
      I don't want to "escape" the brackets, I want to add brackets to the exception. Currently the exception is all lowercase or capital alphabetical letters, and all 10 numbers. I want to also add ] [ to the exception [the two brackets]. How do I do this? Thanks, and I appreciate the response.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by Untam3d
        I don't want to "escape" the brackets, I want to add brackets to the exception.
        guess how you do that.....

        Code:
        preg_match('§^[a-z0-9\[\]]$§i', $char)
        ... by escaping

        Comment

        • Untam3d
          New Member
          • Aug 2009
          • 3

          #5
          Originally posted by Dormilich
          guess how you do that.....

          Code:
          preg_match('§^[a-z0-9\[\]]$§i', $char)
          ... by escaping
          Got it! Thanks a whole lot Dormilich.

          Comment

          Working...