如何检查字符串中包含A-Z或a-z或0-9的字符串?(php)

时间:2013-12-10 07:57:19

标签: php

如何识别字符串是否包含a-z或A-Z? 或包含0-9? 我需要一个函数来判断属于字母类型或数字类型的字符串。 非常感谢!!

2 个答案:

答案 0 :(得分:1)

您可以使用PHP上的ctype函数

ctype_alphactype_digit可能就是你要找的东西。

<强>用法:

//For alphabets
if(ctype_alpha('helloworld'))
{
echo "Yeah this is text";
}
//For digits...
if(ctype_digit('45'))
{
echo "Yeah this is a number";
}

答案 1 :(得分:0)

您可以使用正则表达式:

$string = "abasdfBSDFSDFJKJH23409283049";
if (preg_match("^/[A-Za-z0-9]$/", $string)) 
{
    //it matches
}
相关问题