如何检查字符串中的整数和特殊字符


How to check for integers and special charectors in a string?

我正在制作注册页面。每当用户在名称字段中输入数字/特殊字符时,我想显示错误!

那么有没有做同样事情的函数呢?

使用此正则表达式 '/^[a-z][a-z ]*$/i'.它仅匹配字母和空格。

if (!preg_match('/^[a-z][a-z ]*$/i', $firstname)) {
    // firstname is invalid
}

可以使用正则表达式。例:

$str = 'test';
if(!preg_match('/^[a-zA-Z ]+$/', $str)){
    //echo 'invalid';
}

正则表达式演示