Home  >  Article  >  php教程  >  PHP正则判断输入是否字母实例程序

PHP正则判断输入是否字母实例程序

WBOY
WBOYOriginal
2016-06-13 10:04:121239browse

在php中判断是否为纯字母我们可直接使用正则/^[a-zA-Z]$/来验证了,包括大小写字母哦,有需要了解的同学可参考参考。

上代码

 代码如下 复制代码

header('Content-type: text/html; charset=utf-8');

$str = "dasdadsfsadASDSADS";

if (preg_match('/^[a-zA-Z]+$/',$str))
{
 echo $str."是字母";
}
else
{
 echo $str."不是字母";
}

?>

这个就是代码

 代码如下 复制代码
preg_match('/^[a-zA-Z]+$/',$str)

如果是字母则返回TRUE,否则返回FALSE

其它的判断

 代码如下 复制代码

if(preg_match("/^d*$/",   "4312"))
{
echo   "全数字
";
}

if(preg_match("/^[a-z]*$/i",   "fdsFDfd"))
{
echo   "全字母
";
}

if(preg_match("/^[a-zd]*$/i",   "fd4fd34"))
{
echo   "有数字有字母
";
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn