php - PHP 中不同的返回值

标签 php regex pcre

我正在调查 another question随之而来的是这个问题。

我想知道为什么在使用 PHP >= 5.3.4 时使用 \p{L} 会导致 false 而在早期版本中会导致 true

print_r(preg_match("@^\d+\s+\p{L}+\s+\d+$@", "20 Août 2014"));

See online

更新#1

\p{L} 应该在 PCRE 8.30 到 8.34 中按预期工作,因为我可以在 RegexBuddy 等环境中进行测试:

enter image description here

因此 PHP 5.4.14 (PCRE 8.30) 到 5.6 (PCRE 8.34) 应该实现相同的结果(因为我找不到对 PHP PCRE 包所做的任何自定义更改):

enter image description here

根据@user1578653 answer ,使用字母 Å0xc5 十六进制代码会有不同的输出,但是 it won't (!) 但它 should match .

最佳答案

从 v 5.3.4 (http://php.net/ChangeLog-5.php) 的 PHP 变更日志看来,其中一项变更是“将捆绑的 PCRE 升级到 8.10 版。(Ilia)”。

PCRE v8.10 的变更日志 ( http://www.pcre.org/changelog.txt ) 提到了关于\p 修饰符的几件事,特别是第 12 点和第 15 点。也许这些与您的问题有关?

更新

我又做了一些测试,我认为这是造成差异的原因。 PCRE 变更日志中的第 15 点指出:

If a repeated Unicode property match (e.g. \p{Lu}*) was used with non-UTF-8 input, it could crash or give wrong results if characters with values greater than 0xc0 were present in the subject string. (Detail: it assumed UTF-8 input when processing these items.)

如果您尝试用任何小于 unicode 0xc0 的字符替换您的 'û' 字符,您将在所有版本的 PHP 上得到相同的结果。如果您将该字符替换为任何等于或大于 0xc0 的字符,您就会看到所看到的 PHP 版本之间的差异。所以一定是PCRE库这次更新引起的!

关于php - PHP 中不同的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25682785/

相关文章:

c++ - pcre2_substitute() 函数支持多少捕获组?

php - 如何将用户添加到 Facebook 自定义受众

php - 用于 Sendgrid API 的 php 的 curl -d 选项

php - 使用复选框更新数据库列

python - 匹配前后如何匹配和创建字典

python - 如何提取由点分隔的数字,后跟一组特定的单词正则表达式 python?

php - number_format 和负数的问题

python - 使用正则表达式从字符串中仅提取 Unicode 字符

regex - 多行模式下字符串开头和结尾的正则表达式

regex - R 正则表达式中 Perl/x(忽略空格)模式的等价物