List of special character
Results 1 to 9 of 9

Thread: List of special character

  1. #1
    Junior Member
    Join Date
    May 2013
    Location
    http://greenavis.com/
    Posts
    3

    List of special character

    Hello,

    I am developing a simple form to collect user information. I want a sample PHP code, that will validate the input string to display whether it has any of the below list of special character in the string.

    Special Characters ( - & ' " / \ % * # )

    Can one of please help me. More detailed info below:

    I have a form that submits the input to a PHP modules using Ajax Post. When my PHP modules reads the POST values, it should validate to check if the special characters are available, if it is available then display "No special characters allowed"


    Anyones help will be so greatful

    Thank you.
    Last edited by bradgrafelman; 05-15-2013 at 06:17 PM. Reason: title changed as per OP request

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    I would probably use preg_match() with a pattern like:
    Code:
    @[&'"/\\%*#()-]@
    However, doing this begs the question: What makes these 11 specific characters so "special?" Are you sure a whitelist wouldn't be more effective than a blacklist in your situation?

    EDIT: Also, welcome to PHPBuilder!
    Last edited by bradgrafelman; 05-15-2013 at 06:21 PM.

  3. #3
    Junior Member
    Join Date
    May 2013
    Location
    http://greenavis.com/
    Posts
    3
    Thank you, i will try to reconsider...

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,932
    I'm not sure exactly what it is that you seek to do, but it sounds like you want to screen a user input string to prevent them from including any of those special characters. bradgrafelman has a good point in suggesting that you might want to consider a whitelist. A whitelist would be easier if you want to permit only alphanumeric characters and spaces or something. On the other hand, I can't help but wonder if the preg_* functions would work well with all languages. E.g., can you specify a character range in kanji or mandarin like you can specify [A-Z]?

    At any rate, if you want to detect the presence of those characters above, I would recommend something like this:
    PHP Code:
    $forbidden_chars = array(
      
    "(",
      
    "-",
      
    "&",
      
    "'",
      
    "\"",
      
    "/",
      
    "\\",
      
    "%",
      
    "*",
      
    "#",
      
    ")"
    );

    // you would need to change appropriately for your AJAX script
    $user_input "nothing to see he're";

    foreach(
    $forbidden_chars as $fc) {
      if (
    strpos($user_input$fc) !== FALSE) {
        die(
    $fc " is forbidden!");
      }
    }

    // if you reach this point in your code, you can be sure that there are no forbidden chars in $user_input 
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,432
    Quote Originally Posted by sneakyimp View Post
    can you specify a character range in kanji or mandarin like you can specify [A-Z]?
    I don't see why not. "[A-Z]" just means "look for any byte value that is in the interval [0x41, 0x5A]." Turn on multibyte support, change the values in the interval, and now you've got a range from "あ" to "を".

  6. #6
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,932
    Quote Originally Posted by bradgrafelman View Post
    I don't see why not. "[A-Z]" just means "look for any byte value that is in the interval [0x41, 0x5A]." Turn on multibyte support, change the values in the interval, and now you've got a range from "あ" to "を".
    I suppose you are right. That was a lack of concentration/creativity on my part. I first thought that some chars would be single-byte and others would be double-byte but upon further reflection there is still an ordinal sequence to UTF-8 or whatever...I guess. I guess I also wonder if one might specify a range for english patterns that extends starting from A past the Z but then realize that pretty much all the chars immediately following Z are the very brackets and caret chars that are reserved chars for the specification of such a range.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  7. #7
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,932
    if anyone's curious, it looks like you can specify a very wide range of chars using that sort of regex notation:
    PHP Code:
    $pattern '/[!-~]/'// this would include nearly all printable ASCII characters 
    Beyond that, I don't think there's enough coffee in the office today to goad my brain into thinking more about this.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  8. #8
    Junior Member
    Join Date
    May 2013
    Location
    http://greenavis.com/
    Posts
    3
    Thanks, Sneakyimp. Thats just right i was looking for!
    _________________________________
    -Dan greenavis

  9. #9
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,910
    Quote Originally Posted by sneakyimp View Post
    if anyone's curious, it looks like you can specify a very wide range of chars using that sort of regex notation:
    PHP Code:
    $pattern '/[!-~]/'// this would include nearly all printable ASCII characters 
    Beyond that, I don't think there's enough coffee in the office today to goad my brain into thinking more about this.
    That does cover all printable ASCII characters, unless you count space (and therefore tab, newline, formfeed, etc.) as printable. If you do,
    PHP Code:
    $pattern '/[ -~\x09-\x13]/'
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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