views:

29

answers:

2

I am using pspell to spell check some words. However if the word is something like G3523B it clearly is not a misspelled word but pspell changes it to GB. I would like somehow to qualify a word as a word before trying to spell check it. Maybe checking to see if the string contains any numbers or special characters.

So what is the best way to check a string for special chars or digits?

(if someone has a better idea to achieve what I am after please share)

+1  A: 

How about using a regex:

if (preg_match('/[^z-zA-Z]+/', $your_string, $matches))
{
  echo 'Oops some number or symbol encountered !!';
}
else
{
  // everything fine...
}
Sarfraz
You beat me to a similar solution: `$is_word = (preg_match('/[^a-z]/i', $string) == 0)`;
Mike
Thanks, I think I should also include `'` for words like don't... so should I also include the `\` in my character class since I am using mysql_real_escape_string? Thanks!
John Isaacks
@John Isaacks: I think that is not needed, we are checking anything that is not alphabet, so it will also be rejected.
Sarfraz
@sAc, @John Isaacks: If you want to allow apostrophes (or anything else) to be allowed through for spell checking, then yes, you do need to include it in the list of allowed characters. For example, to allow letters and apostrophes: `$is_word = (preg_match('/[^a-z']/i', $string) == 0);`
Mike
@Mike, @John Isaacks: yes if you also want to whilelist that character you should put that in character class not otherwise.
Sarfraz
+1  A: 

If you just want to check whether the string $input consists only of characters a-z and A-Z you can use the following:

if(!preg_match('/^\[a-zA-Z]+$/',$input)) {
   // String contains not allowed characters ...
}
Javaguru