CodeIgniter中文单词格式验证

时间:2021-11-05 08:15:47

I am using library of Form Validation in CodeIgniter. Below config try to include all Number, English words, Chinese words and space. But it's not work.

我在CodeIgniter中使用表单验证库。下面的配置尝试包括所有数字,英文单词,中文单词和空格。但它不起作用。

$config = array(
                array(
                       'field' => 'keywords',
                       'label' => 'keywords',
                       'rules' => 'regex_match[/[a-zA-Z0-9 \u4e00-\u9fa5]+$/]'
                    )
                );

However, if I deduce '\u4e00-\u9fa5', it's work.

但是,如果我推断'\ u4e00- \ u9fa5',它就可以了。

$config = array(
                    array(
                           'field' => 'keywords',
                           'label' => 'keywords',
                           'rules' => 'regex_match[/[a-zA-Z0-9 ]+$/]'
                        )
                    );

2 个解决方案

#1


1  

There are three issues in the regex you have:

你有正则表达式有三个问题:

  • The validation regex should start matching at the start of the string, thus, you need the start of string anchor ^ or \A. Also, it is advisable to replace $ with the very end of the string anchor \z (as $ also matches before the final newline symbol in a string).
  • 验证正则表达式应该在字符串的开头匹配,因此,您需要字符串锚点^或\ A的开头。另外,建议用字符串anchor \ z的末尾替换$(因为$也匹配字符串中的最后换行符号之前)。
  • Revo is right, \uXXXX notation is not supported by PHP regex engine. However, you do not have to specify the range of Unicode code points here. Chinese characters in PHP PCRE regex can be defined with a Unicode property \p{Han}.
  • Revo是对的,PHP正则表达式引擎不支持\ uXXXX表示法。但是,您不必在此处指定Unicode代码点的范围。 PHP PCRE正则表达式中的中文字符可以使用Unicode属性\ p {Han}定义。
  • For a PCRE regex to become Unicode aware, you need to use the /u modifier.
  • 要使PCRE正则表达式能够识别Unicode,您需要使用/ u修饰符。

So, use

所以,使用

/\A[a-zA-Z0-9\s\p{Han}]+\z/u

Or (a tiny bit less secure),

或者(稍微不那么安全),

/^[a-zA-Z0-9\s\p{Han}]+$/u

#2


0  

PCRE does not support the \uFFFF syntax. Use \x{FFFF} instead.

PCRE不支持\ uFFFF语法。请改用\ x {FFFF}。

/[a-zA-Z0-9 \x{4e00}-\x{9fa5}]+$/

#1


1  

There are three issues in the regex you have:

你有正则表达式有三个问题:

  • The validation regex should start matching at the start of the string, thus, you need the start of string anchor ^ or \A. Also, it is advisable to replace $ with the very end of the string anchor \z (as $ also matches before the final newline symbol in a string).
  • 验证正则表达式应该在字符串的开头匹配,因此,您需要字符串锚点^或\ A的开头。另外,建议用字符串anchor \ z的末尾替换$(因为$也匹配字符串中的最后换行符号之前)。
  • Revo is right, \uXXXX notation is not supported by PHP regex engine. However, you do not have to specify the range of Unicode code points here. Chinese characters in PHP PCRE regex can be defined with a Unicode property \p{Han}.
  • Revo是对的,PHP正则表达式引擎不支持\ uXXXX表示法。但是,您不必在此处指定Unicode代码点的范围。 PHP PCRE正则表达式中的中文字符可以使用Unicode属性\ p {Han}定义。
  • For a PCRE regex to become Unicode aware, you need to use the /u modifier.
  • 要使PCRE正则表达式能够识别Unicode,您需要使用/ u修饰符。

So, use

所以,使用

/\A[a-zA-Z0-9\s\p{Han}]+\z/u

Or (a tiny bit less secure),

或者(稍微不那么安全),

/^[a-zA-Z0-9\s\p{Han}]+$/u

#2


0  

PCRE does not support the \uFFFF syntax. Use \x{FFFF} instead.

PCRE不支持\ uFFFF语法。请改用\ x {FFFF}。

/[a-zA-Z0-9 \x{4e00}-\x{9fa5}]+$/