PHP中的“正则表达式太大”错误

前端之家收集整理的这篇文章主要介绍了PHP中的“正则表达式太大”错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在研究一个相对复杂,非常大的正则表达式.它目前有41,127个字符,并且可能会增加一些,因为可能会添加其他案例.我开始在 PHP中出现此错误

preg_match_all(): Compilation Failed: regular expression is too large at offset 41123

有没有办法增加尺寸限制?其他地方建议的以下设置不起作用,因为这些适用于数据大小而不是正则表达式大小:

ini_set("pcre.backtrack_limit","100000000");
ini_set("pcre.recursion_limit","100000000");

或者,有没有办法在正则表达式中定义一个可以在正则表达式中的不同位置重复的“子模式变量”? (我不是在谈论重复使用*或者甚至重复匹配的“1”)?我实际上使用的PHP变量包含在正则表达式中的几个地方重复的子模式,但是这会导致正则表达式在传递给PRCE函数之前扩展.

这是一个复杂的正则表达式,不能被使用strpos或类似的更简单的关键字搜索所取代,如this link所示.

我宁愿避免将其拆分为|的子表达式并尝试分别匹配子表达式,因为大小的减少将是适度的(顶级|只有2或3),这将使进一步的开发变得复杂.

我并不反对可能有更好的方法做到这一点的评论,但我会在这里回答这个问题.

您可以增加正则表达式的最大大小,但只能通过自己重新编译PHP.因此,您的代码根本不可移植,如果您使用的是预编译的二进制文件,那么您就不走运了.

也就是说,我建议找一个匹配的替代品.

有关评论,请参见pcre_internal.h.

PCRE keeps offsets in its compiled code as 2-byte quantities
(always stored in big-endian order) by default. These are used,for
example,to link from the start of a subpattern to its alternatives
and its end. The use of 2 bytes per offset limits the size of the
compiled regex to around 64K,which is big enough for almost
everybody. However,I received a request for an even bigger limit.
For this reason,and also to make the code easier to maintain,the
storing and loading of offsets from the byte string is now handled
by the macros that are defined here.

The macros are
controlled by the value of LINK_SIZE. This defaults to 2 in the
config.h file,but can be overridden by using -D on the command line.
This is automated on Unix systems via the “configure” command.

因此,您可以从PHP代码分发中编辑ext / pcre / pcrelib / config.h以增加大小限制,或在编译时指定它./configure -DLINK_SIZE = 4

编辑:如果您正在尝试匹配/解析HTML,我建议使用DOMDocument解析HTML,然后遍历DOM树或构建XPATH以找到您要查找的内容.

原文链接:https://www.f2er.com/php/135599.html

猜你在找的PHP相关文章