重音字符form_validation codeigniter

问题描述 投票:0回答:2

我如何扩展我现有的form_validaion类,使其接受重音字符我正在使用codeigniter,这是MY_Form_validation到目前为止:

class MY_Form_validation extends CI_Form_validation{
    public function __construct(){
        parent::__construct();
    }
    public function alpha_dash($str){
        return (!preg_match("/^([-a-z0-9 _-])+$/i", $str)) ? FALSE : TRUE;
    }
}

通过重音字符我的意思是:

“éàèèêêîâ€ÃÃëäù...”

提前致谢。

php regex validation codeigniter-2
2个回答
3
投票

只需在课程中添加想要的字符:

[a-z0-9 _àèéù-]

或使用unicode属性:

[\pL\pN_ -]

\pL代表任何数字的任何字母\pN

例如:

$str = 'abcèéù';
echo preg_match('/^[\pL\pN_ -]+$/', $str) ? 'TRUE' : 'FALSE';

输出:

TRUE

0
投票

这是一个老问题,但接受的答案是不完整的。搜索模式\ pL受setLocale配置的影响。为了让它在任何地方都能工作,你必须使用unicode。像这样的东西:

public function alpha_dash($str){
    return (bool) preg_match('/^[0-9\pL _-]+$/u',$str);
}
© www.soinside.com 2019 - 2024. All rights reserved.