PHP PCRE正则表达式编译错误


PHP PCRE regex compilation error

当我尝试以下操作时:

$searchText = preg_quote($searchText, '/');
$remarks = preg_replace('/'.$searchText.'/i', '<span class="searchText">$0</span>', $remarks);

我得到以下错误信息:

正则表达式在偏移量0处太大

我不知道这意味着什么,为什么我得到它或如何修复它。当我谷歌这个错误信息时,我得到了php.ini设置的参考,我甚至找不到。

正则表达式的大小限制是65539(是的,不是65536)。

请看这里:http://www.pcre.org/pcre.txt

SIZE AND OTHER LIMITATIONS
       There  are some size limitations in PCRE but it is hoped that they will
       never in practice be relevant.
       The maximum length of a compiled pattern is 65539 (sic) bytes  if  PCRE
       is compiled with the default internal linkage size of 2. If you want to
       process regular expressions that are truly enormous,  you  can  compile
       PCRE  with  an  internal linkage size of 3 or 4 (see the README file in
       the source distribution and the pcrebuild documentation  for  details).
       In  these  cases the limit is substantially larger.  However, the speed
       of execution is slower.

从前面的问题来看,您似乎在尝试突出显示用户搜索的单词。如果是这样,你不需要一个正则表达式,你可以这样做:

$remarks = str_replace( $searchText, '<span class="searchText">' . $searchText . '</span>', $remarks);

注意它将突出显示单词中的字符串。如果你想匹配整个单词,我可以更新我的答案