Operation example of PHP preg match regular expression function

高洛峰
Release: 2023-03-04 10:36:02
Original
1168 people have browsed it

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)
Copy after login

PHP preg match正则表达式函数

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);
}
?>
Copy after login

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(&#39;pcre.backtrack_limit&#39;, 999999999);
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!