Home > Backend Development > PHP Tutorial > Use of PHP preg_match regular expression_PHP tutorial

Use of PHP preg_match regular expression_PHP tutorial

WBOY
Release: 2016-07-20 11:03:33
Original
902 people have browsed it

The preg_match() function in PHP is a commonly used function for executing regular expressions. Let me introduce the use of preg_match in detail. ​

Function usage

int preg_match_all ( string pattern, string subject, array matches [, int flags] )

Example 1

The code is as follows Copy code
 代码如下 复制代码

preg_match_all ("|<[^>]+>(.*)]+>|U","example:

this is a test ",$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
?>

 

本例将输出:
example: , example:

this is a test , this is a test

 

preg_match_all ("|<[^>]+>(.*)]+>|U", "example:

this is a test ",$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."n";
print $out[1][0].", ".$out[1][1]."n";
?>

This example will output:
example: , example:
 代码如下 复制代码

// 从 URL 中取得主机名
preg_match("/^(http://)?([^/]+)/i", "http://www.***.net/index.html", $matches);
$host = $matches[2];
// 从主机名中取得后面两段
preg_match("/[^./]+.[^./]+$/", $host, $matches);
echo "domain name is: {$matches[0]}n";
?> 

本例将输出:

domain name is: PPP.NET

this is a test , this is a test

Example 2

Extract domain name from URL

The code is as follows Copy code

// Get hostname from URL
preg_match("/^(http://)?([^/]+)/i", "http://www.***.net/index.html", $matches);
$host = $matches[2];
// Get the last two segments from the host name
preg_match("/[^./]+.[^./]+$/", $host, $matches);
echo "domain name is: {$matches[0]}n";
?>

This example will output: domain name is: PPP. NET

 代码如下 复制代码
ini_set('pcre.backtrack_limit', 999999999);

preg_match string length problem

The preg_match regular method extracts the target content. There are problems 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.recursion_limit', 99999);

Note: This parameter is available after PHP 5.2.0 version.

In addition, let’s talk about

: pcre.recursion_limitpcre.recursion_limit is the recursion limit of PCRE. If this item is set to a large value, the available stacks of all processes will be consumed, and eventually PHP will crash. You can also limit it by modifying the configuration: In actual project applications, it is best to limit the memory: ini_set('memory_limit', '64M'); , which is more secure.
http://www.bkjia.com/PHPjc/445283.htmlwww.bkjia.com
truehttp: //www.bkjia.com/PHPjc/445283.htmlTechArticleThe preg_match() function in php is a commonly used function for executing regular expressions. Let me give Let’s introduce the use of preg_match in detail. Function usage int preg_match_all ( stri...
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