Loading...
X

PHP regular expressions don’t work with very long strings (SOLVED)

preg_replace, preg_match_all, and preg_match functions don't work with long strings

When trying to search in PHP using regular expressions using preg_replace, preg_match_all, and preg_match (or other regular expression search functions), the following problem may occur: the string being searched for exactly contains a substring that matches the search pattern; but instead of outputting the matched strings, the function finds nothing. No errors are output either.

Since PHP 5.2, there is a limit on the size of the text that PCRE functions can be applied to. By default, the maximum text size for which PCRE functions work is 100k. If this limit is exceeded, no error messages will be output, but nothing will be found either.

How to increase the string limit for regular expression search

A logical question arises, how to increase the maximum string length limit for PCRE functions.

You can increase the maximum string length for regular expression search in PHP in the php.ini file.

First, find this file:

locate php.ini

Example file location for Debian-based Linux distributions (Ubuntu, Linux Mint, Kali Linux) — please note that the file location depends on the PHP version:

/etc/php/8.2/apache2/php.ini

Example file location for Arch Linux-based Linux distributions (Manjaro, BlackArch):

/etc/php/php.ini

Open the php.ini file as root:

sudo gedit /etc/php/php.ini

Find the line with pcre.backtrack_limit:

;pcre.backtrack_limit=100000

Uncomment this line (remove the semicolon at the beginning of the line) and set the desired limit:

Save and close this file.

If you run PHP scripts on the command line, the changes you make should take effect immediately.

If you run scripts using the Apache web server, you need to restart this service.

To restart the Apache service on Debian-based distributions (Ubuntu, Linux Mint, Kali Linux), run the following command:

sudo systemctl restart apache2.service

To restart the Apache service on Arch Linux-based distributions (Manjaro, BlackArch), run the following command:

sudo systemctl restart httpd.service

After this, the changes should take effect.

If your problem is not solved, then try increasing the limit to a higher value.

Is it safe to increase pcre.backtrack_limit

There is an opinion that increasing pcre.backtrack_limit can lead to a stack overflow and crash the Apache service.

Apparently, these bugs have already been fixed, since I do not observe any problems in modern versions of PHP and Apache after increasing pcre.backtrack_limit.

On the contrary, it solved my problem with regular expression searching in very long strings and very large text files.


Leave Your Observation

Your email address will not be published. Required fields are marked *