php - 在 PCRE/PHP 中匹配 Unicode 字母字符

标签 php regex unicode pcre character-properties

我正在尝试为 PHP 中的名称编写一个相当宽松的验证器,我的第一次尝试包括以下模式:

// unicode letters, apostrophe, hyphen, space
$namePattern = "/^([\\p{L}'\\- ])+$/";

这最终会传递给对 preg_match() 的调用。据我所知,这适用于您的原始 ASCII 字母表,但似乎会遇到 Ă 或张等更刺激的字符。

模式本身有问题吗?也许我期望 \p{L} 比我想象的做更多的工作?

或者它是否与输入的传递方式有关?我不确定它是否相关,但我确实确保在表单页面上指定了 UTF8 编码。

最佳答案

我认为问题比这简单得多:您忘记指定 u modifier . Unicode 字符属性为 only available in UTF-8 mode .

你的正则表达式应该是:

// unicode letters, apostrophe, hyphen, space
$namePattern = '/^[-\' \p{L}]+$/u';

关于php - 在 PCRE/PHP 中匹配 Unicode 字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983392/

相关文章:

php - 如果我有 id 如何知道在哪个页码?

php - 处理php而不刷新页面

php - Selenium2 火狐 : use the default profile

java - PrintStream 无法正确打印 unicode 字符 (UTF-16)

c++ - swprintf 在 xcode 中使用 unicode 字符失败,但在 visual studio 中有效

java - UTF-8 到代码点

php - 如何删除此正则表达式输出中的空格?

c# - C# 和 regex101 之间的正则表达式结果不同

javascript - 替换 JavaScript 中的方法

regex - Perl 多行匹配,同时从文件中逐行读取