Reputation: 65
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.
$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.
$config = array(
array(
'field' => 'keywords',
'label' => 'keywords',
'rules' => 'regex_match[/[a-zA-Z0-9 ]+$/]'
)
);
Upvotes: 2
Views: 529
Reputation: 626806
There are three issues in the regex you have:
^
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).\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}
./u
modifier.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
Upvotes: 1
Reputation: 48711
PCRE does not support the \uFFFF
syntax. Use \x{FFFF}
instead.
/[a-zA-Z0-9 \x{4e00}-\x{9fa5}]+$/
Upvotes: 0