开发者

php preg_match utf-8 strange behaviour

开发者 https://www.devze.com 2023-01-15 02:30 出处:网络
I search the internet but I couldn\'t find a proper answer so I try this way. I use this code to validate UTF-8 input. I want to allow printable chars and some specified special chars.

I search the internet but I couldn't find a proper answer so I try this way.

I use this code to validate UTF-8 input. I want to allow printable chars and some specified special chars.

$pattern = '/[^\w\.\-\s\,\&\!\?\(\)\+\_\:\;]+$/u';
$status = @preg_match($pattern, $value);
if (($status === false) || ($status > 0)) {
    return false;
}

Everything wo开发者_开发知识库rks fine, EXCEPT the input string has at the end a non ascii char (eg. é). Then my validation fails, but it should not. I know it might be a silly mistake, but thanks in advance for every proposal.

best regards


Try Unicode character properties:

/[^\p{L}.\-\s,&!?()+_:;]+$/u

Here \p{L} represents any Unicode character that is categorized as a letter.


use \pL to match any letter character

0

精彩评论

暂无评论...
验证码 换一张
取 消