How to check, if a php string contains only english letters and digits?

前端 未结 9 1613
既然无缘
既然无缘 2020-11-30 01:33

In JS I used this code:

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

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

9条回答
  •  天涯浪人
    2020-11-30 02:19

    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
    }
    

提交回复
热议问题