欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php的phalcon框架不能验证中文字符

程序员文章站 2022-03-26 15:39:47
...
直接在php中验证可以验证
$value = '电商';
preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z_\-\x{4e00}-\x{9fa5}]*$/u',$value,$m)

在phalcon的控制器的方法中就不能验证,

public function registerAction()
    {
        if($this->request->isPost())
        {
            $myValidator = new MyValidator();
            $errors = $myValidator->validateUsername($this->request->getPost('user_name'));
       }
    }

下面是调用的函数,

public function validateUsername($value)
    {
        $error = false;
        if(!preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z_\-\x{4e00}-\x{9fa5}]*$/u',$value,$m))
        {
            $error = true;
            print_r('world');
        }
        else
        {
            $pattern = '/[\x{4e00}-\x{9fa5}]/u';
            $str1    = preg_replace($pattern, '', $value, -1, $count);

            $length  = strlen($str1) + $count * 2;
            if (!($length >= 2 && $length 'user name is invalid2.'];
        }
        else
        {
            $return = null;
        }
        return $return;

    }

回复内容:

直接在php中验证可以验证

$value = '电商';
preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z_\-\x{4e00}-\x{9fa5}]*$/u',$value,$m)

在phalcon的控制器的方法中就不能验证,

public function registerAction()
    {
        if($this->request->isPost())
        {
            $myValidator = new MyValidator();
            $errors = $myValidator->validateUsername($this->request->getPost('user_name'));
       }
    }

下面是调用的函数,

public function validateUsername($value)
    {
        $error = false;
        if(!preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z_\-\x{4e00}-\x{9fa5}]*$/u',$value,$m))
        {
            $error = true;
            print_r('world');
        }
        else
        {
            $pattern = '/[\x{4e00}-\x{9fa5}]/u';
            $str1    = preg_replace($pattern, '', $value, -1, $count);

            $length  = strlen($str1) + $count * 2;
            if (!($length >= 2 && $length 'user name is invalid2.'];
        }
        else
        {
            $return = null;
        }
        return $return;

    }

http://*.com/questions/4923319/php-check-if-the-string-has...

相关标签: php phalcon