The preg_match() function in PHP is a commonly used function for executing 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. The
preg_match() function is used to perform regular expression matching and returns 1 successfully, otherwise it returns 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)
Example:
This example matches strings with . and spaces after uppercase letters, Only J. can be matched, because preg_match() will stop matching after one successful match, and will not match again.
<?php $str="Daniel J. Gross Catholic High School A. is a faith and family based community committed to developing Christian leaders through educational excellence in the Marianist tradition."; if(preg_match("/[A-Z]. /",$str,$matches)){ print_r($matches); } ?>
Output result:
Array ( [0] => J. )
Let me introduce preg_match to you below String length problem
preg_match regular extraction of target content, there is a problem of life and death, 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 stack of all processes, and finally 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!