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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Danny Fox
  • 38,659
  • 28
  • 68
  • 94

10 Answers10

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
}
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
  • 5
    yes. it looks more efficient. To look for the first non-in-range character seems more sensible than scanning whole string. – Your Common Sense Feb 19 '12 at 17:23
  • N.B english verison of above link here: http://php.net/manual/en/function.preg-match.php – Phillip Macdonald Sep 23 '14 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 '17 at 02:44
  • @chap could you please provide an example of "non working" string ? – Maxime Pacary Nov 01 '17 at 14:27
  • 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 02 '17 at 00: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. – Maxime Pacary Nov 02 '17 at 08:30
  • @FrostyZ you are right. I didn't know that the ^ negated the rest of the expression, so I didn't understand why I was getting the result. But now I know it makes perfect sense. Sorry. – chap Nov 02 '17 at 22:31
  • Where is the space included in regex – santanu bera Mar 12 '20 at 13:13
34
if(ctype_alnum($string)) {
    echo "String contains only letters and numbers.";
}
else {
    echo "String doesn't contain only letters and numbers.";
}
Ryan
  • 375
  • 1
  • 3
  • 15
vineeth
  • 357
  • 3
  • 2
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;
    }
}
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
shakee93
  • 4,976
  • 2
  • 28
  • 32
11

You can use preg_match() function for example.

if (preg_match('/[^A-Za-z0-9]+/', $str))
{
  // ok...
}
evilone
  • 22,410
  • 7
  • 80
  • 107
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 .

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
HalfWebDev
  • 7,022
  • 12
  • 65
  • 103
5
if(preg_match('/[^A-Za-z0-9]+/', $str)) {
    // ...
}
Vitamin
  • 1,526
  • 1
  • 13
  • 27
2
if (preg_match('/^[\w\s?]+$/si', $string)) {
    // input text is just English or Numeric or space
}
  • 1
    It depends on locale. For me (French, locale fr_fr) `\w` matches all characters with diacritic like `éèçàÀ...` – Toto Sep 05 '18 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
}
Adam Katz
  • 14,455
  • 5
  • 68
  • 83
Saurabh
  • 1
  • 1
-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';
}