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


How to check string contain A-Z or a-z or 0-9 in a string?(php)

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

您可以在PHP 上使用ctype函数

CCD_ 2和CCD_。

用法:

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

您可以使用正则表达式来实现这一点:

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