Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use preg match regular expression function in PHP

Shulou Source: shulou.com Published: 2022-06-02 05:01:13 10月03日 Update

This article mainly introduces how to use the preg match regular expression function in PHP. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

The preg_match() function is used to match regular expressions and returns 1 if successful, or 0 otherwise.

preg_match() stops matching after one successful match. If you want to match all the results, you need to use the preg_match_all() function.

Grammar:

preg_match (pattern , subject, matches) Parameter description pattern Regular expression subject Objects to be matched matches Optional, array storing matching results

Examples:

This instance matches capital letters followed by. A string of characters with spaces can only match up to J. Because preg_match() will stop matching after one successful match, and will not match again.

Output:

Array ( [0] => J. )

The following is an introduction to the preg_match string length problem

preg_match regular extraction target content, dead or alive problems, code measured dead.

Later, I suspected that PHP's preg_match has a string length limit, and sure enough, I found that the value of "pcre.backtrack_limit " is only set to 100000 by default.

Solution:

ini_set('pcre.backtrack_limit', 999999999);

Note: This parameter is available after PHP 5.2.0.

Pcre. recurrence_limit

pcre. recurrence_limit is the recursion limit of PCRE, which, if set to a large value, consumes all available stacks of processes and eventually causes PHP to crash.

You can also restrict it by modifying the configuration:

ini_set('pcre.recursion_limit', 99999);

In actual project applications, it is also best to set the memory limit: ini_set ('memory_limit','64 M'); , so that it is more stable.

Thank you for reading this article carefully. I hope that Xiaobian will share the article "How to use preg match regular expression function in PHP". This article is helpful to everyone. At the same time, I hope that everyone will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

Tags: Regular function expression article success character string result limit parameter instance length problem J. proper very large over and over again code value interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi MySQL Shulou Information Shulou Technology