PHP preg_match 长度 3276 限制

标签 php preg-match

在某些情况下,PHP 的 preg_match 似乎对匹配重复字符有 3276 个字符的限制。

^(.|\s){0,3276}$ 有效,但 ^(.|\s){0,3277}$ 无效。

它似乎并不总是适用,因为 /^(.){0,3277}$/ 有效。

我无法在 PHP 文档或错误跟踪器中的任何地方找到它。数字 3276 似乎有点奇怪,我唯一能想到的是它大约是 32767 的 1/10,这是有符号 16 位整数的极限。

preg_last_error() 返回 0。

我已经在 http://www.phpliveregex.com/ 上重现了这个问题以及我的本地系统和网络服务器。

编辑:看起来我们从代码中得到“警告:preg_match():编译失败:正则表达式在偏移量 16 处太大”,所以它看起来与 PHP preg_match_all limit 是同一个问题。 .

但是,正则表达式本身不是很大...当您有重复的组使其太大时,PHP 是否会进行某种扩展?

最佳答案

为了处理与 Perl 兼容的正则表达式,PHP 只是捆绑了一个 third-party library负责这项工作。您描述的行为实际上是 documented :

The "*" quantifier is equivalent to {0,} , the "+" quantifier to {1,} , and the "?" quantifier to {0,1} . n and m are limited to non-negative integral values less than a preset limit defined when perl is built. This is usually 32766 on the most common platforms.

所以总是有一个硬性限制。为什么您的测试表明 PHP 限制比典型限制小 10 倍?不知道:)

关于PHP preg_match 长度 3276 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926195/

相关文章:

如果密码为 "("或 ")"或 "&",phpmysql 转储不起作用

php - Ruby 通过模板从字符串中选择数据,就像 php preg_match 做的那样

php - 当想要检测多个 url 时 preg_match 不起作用

php - 用图像替换动态文本

java - PHP 在 Java 中的 `preg_match_all` 功能

PHP mySQL - 从数据库的 3 个表中获取数据

php - 在移动设备(ios 和 android)中使用 twilio php 库?

php - mysql_fetch_assoc() : supplied argument is not a valid MySQL

php - 如何在 Laravel 中的嵌套 2 级关系中按列排序?

php安全问题