62

In JS I used this code:

if(string.match(/[^A-Za-z0-9]+/))

but I don't know, how to do it in PHP.

10 Answers 10

108

Use preg_match().

if (!preg_match('/[^A-Za-z0-9]/', $string)) // '/[^a-z\d]/i' should also work.
{
  // string contains only english letters & digits
}
8
  • 5
    yes. it looks more efficient. To look for the first non-in-range character seems more sensible than scanning whole string. Feb 19, 2012 at 17:23
  • N.B english verison of above link here: php.net/manual/en/function.preg-match.php Sep 23, 2014 at 10:22
  • 1
    I just tried this and the use of the ! operator gives wrong results. The code inside the if statement will be executed if the string does not contain only english letters & digits.
    – chap
    Oct 31, 2017 at 2:44
  • 1
    For example if I give the string "{}\" the code inside your if statement will execute. But your comment inside the if statement // string contains only english letters & digits indicates that you want the code inside the if statement to execute only if the string contains a-z A-Z or 0-9, however by using the ! your code does the opposite.
    – chap
    Nov 2, 2017 at 0:30
  • 1
    @chap did you see the ^ char at the beginning of the regex? It means that as soon as an "invalid" char is found, preg_match() returns true. Without the !, the if block would be executed for any "invalid" string. I will retest the whole thing ASAP however & will keep you informed. Nov 2, 2017 at 8:30
33
if(ctype_alnum($string)) {
    echo "String contains only letters and numbers.";
}
else {
    echo "String doesn't contain only letters and numbers.";
}
2
  • Not sure why people are not voting the ctype function. I can't imaging there is a performance issue with the range of ctype functions.
    – Ryan Bayne
    Sep 6, 2014 at 20:05
  • 4
    ctype_alnum are locale depended so it might not work as you wish !
    – thethakuri
    Jun 3, 2016 at 14:38
12

if you need to check if it is English or not. you could use below function. might help someone..

function is_english($str)
{
    if (strlen($str) != strlen(utf8_decode($str))) {
        return false;
    } else {
        return true;
    }
}
11

You can use preg_match() function for example.

if (preg_match('/[^A-Za-z0-9]+/', $str))
{
  // ok...
}
7

Have a look at this shortcut

if(!preg_match('/[^\W_ ] /',$string)) {

}

the class [^\W_] matches any letter or digit but not underscore . And note the ! symbol . It will save you from scanning entire user input .

5
if(preg_match('/[^A-Za-z0-9]+/', $str)) {
    // ...
}
2
if (preg_match('/^[\w\s?]+$/si', $string)) {
    // input text is just English or Numeric or space
}
1
  • 1
    It depends on locale. For me (French, locale fr_fr) \w matches all characters with diacritic like éèçàÀ...
    – Toto
    Sep 5, 2018 at 12:06
1
if(preg_match('/^[A-Za-z0-9]+$/i', $string)){ // '/^[A-Z-a-z\d]+$/i' should work also
// $string constains both string and integer
}

The carrot was in the wrong place so it would have search for everything but what is inside the square brackets. When the carrot is outside it searches for what is in the square brackets.

0

PHP can compare a string to a regular expression using preg_match(regex, string) like this:

if (!preg_match('/[^A-Za-z0-9]+/', $string)) {
    // $string contains only English letters and digits
}
-1

The following code will take care about special characters and spaces

$string = 'hi, how are you ?';
if (!preg_match('/[^A-Za-z0-9 #$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]+/', $string)) // '/[^a-z\d]/i' should also work.
{
    echo 'english';
}else
{
    echo 'non-english';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.