how to test string whether it contains special characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #16
    Originally posted by TheServant
    Thanks for your input, and welcome to Bytes. I think that rather than forbidding every character possible (black-list) it would be better to have a white-list of allowed characters. Also, what about people with hyphenated names?
    Furthermore, that loop is completely unnecessary. Using preg_match() you can do that in 1 line of code.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #17
      Originally posted by hsriat
      If you are talking about HTML special characters, then just do...
      Code:
      if (htmlspecialchars($string) != $string) {
      //it contains special chars.
      }
      hsriat, I think the OP is long gone, this is a very old post that has been dug up and put at the top of the list :(

      Originally posted by Markus
      Furthermore, that loop is completely unnecessary. Using preg_match() you can do that in 1 line of code.
      Amen.

      Comment

      • RodanMedia
        New Member
        • Sep 2009
        • 4

        #18
        Originally posted by hsriat
        If you are talking about HTML special characters, then just do...
        Code:
        if (htmlspecialchars($string) != $string) {
        //it contains special chars.
        }
        I'll have to give this a shot, but I'm facing a deadline here. So this will be an improvement over our beta version - but later. In the meantime, for all of you, I appreciate your input. Unfortunately, I tried a preg_match() and couldn't make it work, and there's no time to piddle with it. What the code does is verifies a userID, then if there are no special characters, and all other fields are inserted properly, establishes a series of database tables, named after the userID, in SQL. This may be a regurgitated thread, but it came in handy when I needed it. We launch our social network site October 12. There's just no time to become a PHP expert in that time frame.

        Now, if I could still program that old Sperry in COBOL ... oh, well.

        Comment

        • RodanMedia
          New Member
          • Sep 2009
          • 4

          #19
          I should add, the user setting up their account determines their userID ... and we're not converting anything in the userID field, it simply becomes the name of several new database tables. Hence, we must forbid certain characters that can't be included in a data-table name.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #20
            You don't have to be an expert to use preg_match. Try this:
            Code:
            if ( ! preg_match(/^[a-zA-Z0-9]+$/", $userID) ) {
            // Display error
            }
            I think that will do all your code does, plus cover EVERY character other than letters and numbers.

            Comment

            • RodanMedia
              New Member
              • Sep 2009
              • 4

              #21
              Thank you! I'll plug this in and run a test. The other code is already live, but this can quickly take its place. I sure picked the wrong week to start learning PHP - LOL!

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #22
                No worries, let us know if you have any problems.

                Comment

                Working...