直接在php中验证可以验证
<code>$value = '电商';
preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z_\-\x{4e00}-\x{9fa5}]*$/u',$value,$m)
</code>在phalcon的控制器的方法中就不能验证,
立即学习“PHP免费学习笔记(深入)”;
<code>public function registerAction()
{
if($this->request->isPost())
{
$myValidator = new MyValidator();
$errors = $myValidator->validateUsername($this->request->getPost('user_name'));
}
}
</code>下面是调用的函数,
<code>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 <= 14))
{
$error = true;
}
}
if($error)
{
$return = ['user_name'=>'user name is invalid2.'];
}
else
{
$return = null;
}
return $return;
}
</code>直接在php中验证可以验证
<code>$value = '电商';
preg_match('/^[a-zA-Z\x{4e00}-\x{9fa5}][a-zA-Z_\-\x{4e00}-\x{9fa5}]*$/u',$value,$m)
</code>在phalcon的控制器的方法中就不能验证,
立即学习“PHP免费学习笔记(深入)”;
<code>public function registerAction()
{
if($this->request->isPost())
{
$myValidator = new MyValidator();
$errors = $myValidator->validateUsername($this->request->getPost('user_name'));
}
}
</code>下面是调用的函数,
<code>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 <= 14))
{
$error = true;
}
}
if($error)
{
$return = ['user_name'=>'user name is invalid2.'];
}
else
{
$return = null;
}
return $return;
}
</code>
http://stackoverflow.com/questions/4923319/php-check-if-the-string-has...
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号