how to test string whether it contains special characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    how to test string whether it contains special characters

    how to test string whether it contains special characters
    --------------------------------------------------------------------------------

    please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid ex for special characters (",',#,!,>,< ,?) except any letters for other languages ?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Try preg_match()

    [php]
    /*
    - Using preg_match() we'll check for illegal characters:
    */
    $_string = "lolzz#";
    if(preg_match('/[^a-zA-Z0-9_-]+/', $_string))
    # checks for anything BUT: a through z (upper and lower case) 0 - 9, underscore _ and hypen -
    {
    echo "String contained illegal characters";
    }
    else
    {
    echo "String was legal.";
    }
    [/php]
    If you don't understand, then check out the php.net website.

    Regards

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Originally posted by smartic
      how to test string whether it contains special characters
      --------------------------------------------------------------------------------

      please help dear experts on how to test string especially in username for example wheter it contains special charater and will return invalid ex for special characters (",',#,!,>,< ,?) except any letters for other languages ?
      Just my curiosity: by 'foreign letters' do you mean Unicode (UTF-8) characters or just <255 characters?

      Ronald

      Comment

      • smartic
        New Member
        • May 2007
        • 150

        #4
        if i use preg_match or ereg to catch special characters it deal with other languages like arabic as sepecial characters how can i deal with that?

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Originally posted by smartic
          if i use preg_match or ereg to catch special characters it deal with other languages like arabic as sepecial characters how can i deal with that?
          Now you are talking about Unicode characters. I.e. for arabic you would use an expression like (just an example)[php]$arabic=preg_ma tch('/[\x{600}-\x{6FF}]/u', $content)[/php]For searching Japanese circled numbers 1-9[php]preg_match('/[\x{2460}-\x{2468}]/u', $str);[/php]
          I don't know enough of regular expressions and Unicode to say much more about it. So I hope someone schooled in Reg ex and Unicode can help you any further.

          Ronald

          Comment

          Working...