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.

    Cant edit my own thread name, accidentaly inserted wrong character. Please, replace it with L (List) 🙁

      I would probably use [man]preg_match/man with a pattern like:

      @[&'"/\\%*#()-]@

      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!

        Thank you, i will try to reconsider...

          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:

          $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
          
            sneakyimp;11028265 wrote:

            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 "を".

              bradgrafelman;11028267 wrote:

              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.

                if anyone's curious, it looks like you can specify a very wide range of chars using that sort of regex notation:

                $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.

                  sneakyimp;11028275 wrote:

                  if anyone's curious, it looks like you can specify a very wide range of chars using that sort of regex notation:

                  $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,

                  $pattern = '/[ -~\x09-\x13]/';
                  
                    5 days later
                    5 days later

                    Thank you "Sneakyimp" for your reply. I am also looking for special character to add in my project.

                      Write a Reply...