Home  >  Article  >  Backend Development  >  Operation example of PHP preg match regular expression function

Operation example of PHP preg match regular expression function

高洛峰
高洛峰Original
2017-01-09 10:13:151462browse

The preg_match() function in php is a commonly used function used to execute regular expressions. Regular expressions are used in almost all programming languages. This example introduces the application of the regular expression preg_match function in PHP.

preg_match() function is used for regular expression matching, returning 1 successfully, otherwise returning 0.

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

Syntax:

preg_match (pattern , subject, matches)

PHP preg match正则表达式函数的操作实例

Example:

This example matches strings with uppercase letters followed by . and spaces, Only J. can be matched, because preg_match() will stop matching after one successful match, and will not match again.

##Output result:

Array ( [0] => J. )

Let’s introduce preg_match to you String length problem

preg_match regular extracts the target content, there is a problem with life and death, and the code is tested to death.

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

Solution:

ini_set('pcre.backtrack_limit', 999999999);

Note: This parameter is available after PHP 5.2.0 version.

In addition, let’s talk about: pcre.recursion_limit

pcre.recursion_limit is the recursion limit of PCRE. If this item is set to a large value, it will consume the available stacks of all processes and eventually cause PHP to crash. .

You can also limit it by modifying the configuration:

ini_set('pcre.recursion_limit', 99999);

In actual project applications, it is best to limit the memory. : ini_set('memory_limit', '64M'); , this is more secure.

For more related articles on operation examples of PHP preg match regular expression function, please pay attention to PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn