Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to tell if a string contains any special characters? [duplicate]

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)

like image 355
JD Isaacks Avatar asked Jul 15 '10 13:07

JD Isaacks


2 Answers

How about using a regex:

if (preg_match('/[^a-zA-Z]+/', $your_string, $matches))
{
  echo 'Oops some number or symbol encountered !!';
}
else
{
  // Everything fine... carry on
}
like image 122
Sarfraz Avatar answered Sep 26 '22 08:09

Sarfraz


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 ...
}
like image 31
Javaguru Avatar answered Sep 22 '22 08:09

Javaguru