使用preg_match匹配字符串

ria*_*iad 1 php regex

我在$abc变量中有文本.

现在我想检查文本是否只能包含字符(a-z, A-Z, 0-9).如果他们有除了那些之外的任何字符,那么应该返回"输出".

我怎样才能做到这一点?

example: $abc = "this is @ text"; // no match 
Run Code Online (Sandbox Code Playgroud)

Yos*_*shi 6

就像是:

$abc = "this is @ text";
if (!preg_match('/^[a-z0-9]*\z/i', $abc)) {
  echo 'bad';
}
Run Code Online (Sandbox Code Playgroud)

关于Jame C的评论,这里是倒置的案例:

$abc = "this is @ text";
if (preg_match('/[^a-z0-9]/i', $abc)) {
  echo 'bad';
}
Run Code Online (Sandbox Code Playgroud)