开发者

Match string using preg_match

I have text in a $abc variable.

Now I want开发者_StackOverflow社区 to check that text can hold only characters (a-z, A-Z, 0-9). If they have any character except those, then "a output" should be returned.

How can I do that?

example: $abc = "this is @ text"; // no match 


Something like:

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

With regards to Jame C's comment, here is the inverted case:

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


if ( !preg_match('#^[a-zA-Z0-9]*$#', $abc) ) {
    // wrong chars spotted
}


you should be able to evaluate

preg_match('/[^A-Za-z0-9]/', $myString)

if you don't mind spaces and underscores being in there too then you could use this:

preg_match('/\W/', $myString)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜