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)
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.
<?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’s introduce preg_match to you String length problempreg_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);