在PCRE/PHP中匹配Unicode字母字符 [英] Matching Unicode letter characters in PCRE/PHP
问题描述
我正在尝试为PHP中的名称编写一个合理的验证器,而我的第一次尝试包括以下模式:
I'm trying to write a reasonably permissive validator for names in PHP, and my first attempt consists of the following pattern:
// unicode letters, apostrophe, hyphen, space
$namePattern = "/^([\\p{L}'\\- ])+$/";
这最终传递给对preg_match()
的调用.据我所知,这可以与您的原始ASCII字母一起使用,但似乎在诸如Ă或张之类的易变字符上起作用.
This is eventually passed to a call to preg_match()
. As far as I can tell, this works with your vanilla ASCII alphabet, but seems to trip up on spicier characters like Ă or 张.
模式本身是否存在问题?也许我期望\p{L}
做的工作比我想像的还要多?
Is there something wrong with the pattern itself? Perhaps I'm expecting \p{L}
to do more work than I think it does?
还是与输入的传递方式有关?我不确定是否相关,但是我确实确保在表单页面上指定UTF8编码.
Or does it have something to do with the way input is being passed in? I'm not sure if it's relevant, but I did make sure to specify a UTF8 encoding on the form page.
推荐答案
我认为问题要比这简单得多:您忘记指定u
仅在UTF-8模式下可用.
I think the problem is much simpler than that: You forgot to specify the u
modifier. The Unicode character properties are only available in UTF-8 mode.
您的正则表达式应为:
// unicode letters, apostrophe, hyphen, space
$namePattern = '/^[-\' \p{L}]+$/u';
这篇关于在PCRE/PHP中匹配Unicode字母字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!