Check string for special chars help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gamernaveen
    New Member
    • Oct 2007
    • 28

    #1

    Check string for special chars help

    I wanted to include a string invalid character check in my application.
    Like the field comment , if a user inserts any special characters
    (Except '[' , ']' , '@' , '/' , '=' and numbers - alphabets) , the function should return true , if the string contains no special char in any position , the function should return false.

    Can anyone design a function to check the special chars ?
    Thanks so much.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    So, there HAS to be a special char in the string?
    Ok, so, you can preg_match()

    [code=php]
    <?php
    $_string = "thisisagoodstr ing*";
    $_stringbad = "thisisabadstri ng";

    if(preg_match(' #[^a-zA-Z0-9]#', $_string))
    {
    echo "special char found!";
    }
    ?>
    [/code]

    Comment

    • dlite922
      Recognized Expert Top Contributor
      • Dec 2007
      • 1586

      #3
      Originally posted by gamernaveen
      I wanted to include a string invalid character check in my application.
      Like the field comment , if a user inserts any special characters
      (Except '[' , ']' , '@' , '/' , '=' and numbers - alphabets) , the function should return true , if the string contains no special char in any position , the function should return false.

      Can anyone design a function to check the special chars ?
      Thanks so much.
      On your free time, I recommend looking into regular expressions. They will seam over-whelming at first and many sites will go deep into it, but learning the basics will help you alot.

      In coding (your IDE's Find and Replace feature) AND in your code.

      Have fun,


      -Dan

      Comment

      Working...