In PHP, the Preg_match () function is a common function used to execute regular expressions. Regular expressions are used in almost all programming languages, and this example introduces the application of the Preg_match function of the PHP positive expression.
The Preg_match () function is used to match a regular expression, successfully returning 1, otherwise returning 0.
The Preg_match () match will stop the match once successful, and if you want to match all the results, you need to use the Preg_match_all () function.
Grammar:
Preg_match (pattern, subject, matches)
Parameters |
Describe |
Pattern |
Regular expressions |
Subject |
Objects that need to match the retrieved |
Matches |
Optionally, storing an array of matching results |
Instance:
This instance matches a string with a. And a space followed by a capital letter that can only be matched to J., because the Preg_match () match succeeds once and then stops the match and no more matches.
<?php
$str = "Daniel J. Gross Catholic High School A. are a faith and family based community committed to developing C Hristian leaders through educational excellence in the Marianist tradition. ";
if (Preg_match ("/[a-z]. /", $STR, $matches)) {
print_r ($matches);
}
? >
Output results:
Array ([0] => J.)
Let me introduce you to Preg_match string length problem
Preg_match is to extract the target content, dead and dead have problems, the code measured to death.
Later suspected PHP Preg_match has string length limit, sure enough, found that the value of "Pcre.backtrack_limit" by default only set 100000.
Solution:
Ini_set (' Pcre.backtrack_limit ', 999999999);
Note: This parameter is available after PHP version 5.2.0.
In addition to say about: Pcre.recursion_limit
Pcre.recursion_limit is the recursive limit of the pcre, which, if set to a large value, consumes all the available stacks of the process and eventually causes PHP to crash.
You can also restrict by modifying the configuration:
Ini_set (' Pcre.recursion_limit ', 99999);
In the actual project application, it is best to limit the memory setting: Ini_set (' memory_limit ', ' 64M '); , which is more secure.