如何使用REGEXP验证名称


How to validate name using REGEXP

我试图验证我的名字的文本框,所以即使他们通过它,它仍然不会接受带有特殊字符的名称,如"!@#$%^&*()_"和数字,这是我的代码:

  public function updateuserAction(){
    $this->view->disable();
    $get = $this->request;
    $key = $get->get('key');
    $email = $get->get('email');
    $password = $get->get('password');
    $realName = $get->get('realName');
    $type = $get->get('userType');
        if(filter_var($realname, FILTER_VALIDATE_REGEXP, array("options" => array("regexp" => "/^[a-zA-Z]+$/")))){
        $url = $this->apiUrl."user/update?q=(key:$key,email:".urlencode($email).",password:".urlencode($password).",realName:".urlencode($realName).",userType:".urlencode($type).")&envelope=false";
        $ch = curl_init($url);
        curl_setopt_array($ch, $this->curlopt);
        echo  curl_exec($ch);
        }else {
        echo "Failed";
        }
}

这是我的代码,我不知道如何在这里插入验证。每当我在这里添加验证代码时,它总是给我一个错误消息。我是这一行的新手,所以如果能帮忙的话,对我来说意义重大。谢谢!

我已经添加了试用代码

php正则表达式匹配函数为- preg_match。

你的问题有点模糊,但从我可以收集你想验证用户名只包含字母字符。因此,尝试使用

if (preg_match("^[a-zA-Z]+$") == false)
{
   echo "Username is invalid.";
   die(); // or some other error handling code
}

啊,现在我知道是什么让我的代码总是失败--只是因为$realname到$realname --我忘了它是大小写敏感的。D