Summary of PHP Regular Expression Handling Function of PCRE Function Examples
- 2021-12-11 07:15:54
- OfStack
In this paper, an example is given to describe the PHP regular expression processing function. Share it for your reference, as follows:
Sometimes you need to match in a specific business scenario, or extract a key piece of information, such as matching a link in a web page,
Regular matching may be used when extracting 1 data.
The following is an introduction to some commonly used regular processing functions in php.
1. preg_replace($pattern,$replacement,$subject)
Performs a search and replace of 1 regular expression.
<?php
echo "<pre>";
$str = "12,34:56;784;35,67:897:65";
// Request that the above :,; Change them into spaces
print_r(preg_replace("/[,;:]/"," ",$str));
?>
Output
12 34 56 784 35 67 897 65
2. preg_match($pattern,$subject, & $matches)
Perform a match regular expression
<?php
echo "<pre>";
$str = "<a href=\"https://www.baidu.com\"> Group buying goods </a>";
// Match the link address
preg_match("/<a href=\"(.*?)\">.*?<\/a>/",$str,$res);
print_r($res);
?>
Output
Array
(
[0] = > Group buying goods
[1] = > https://www.baidu.com
)
3. preg_match_all($pattern,$subject, & $matches)
Perform a global regular expression match
<?php
echo "<pre>";
$str=<<<EOF
<div>
<a href="index.php" rel="external nofollow" > Home page </a>
<a href="category.php?id=3" rel="external nofollow" >GSM Mobile phone </a>
<a href="category.php?id=4" rel="external nofollow" > Dual-mode handset </a>
<a href="category.php?id=6" rel="external nofollow" > Mobile phone accessories </a>
</div>
EOF;
// Use global regular matching
preg_match_all("/<a href=\"(.*?)\">(.*?)<\/a>/s",$str,$res);
print_r($res);
?>
Output
Array
(
[0] = > Array
(
[0] = > Home page
[1] = > GSM mobile phone
[2] = > Dual-mode handset
[3] = > Mobile phone accessories
)
[1] = > Array
(
[0] = > index.php
[1] = > category.php?id=3
[2] = > category.php?id=4
[3] = > category.php?id=6
)
[2] = > Array
(
[0] = > Home page
[1] = > GSM mobile phone
[2] = > Dual-mode handset
[3] = > Mobile phone accessories
)
)
4. preg_split($pattern,$subject)
Separate strings by a regular expression
<?php
echo "<pre>";
$str = "12,34:56;784;35,67:897:65";
// Separated string
$arr = preg_split("/[,;:]/",$str);
print_r($arr);
?>
Output
Array
(
[0] = > 12
[1] = > 34
[2] = > 56
[3] = > 784
[4] = > 35
[5] = > 67
[6] = > 897
[7] = > 65
)
5. preg_quote($str)
Escape regular expression characters
Regular expression special characters are:.\ + *? [^] $() {} =! < > : -
<?php
echo "<pre>";
echo preg_quote("(abc){10}");// Add before the characters of each regular expression syntax 1 Backslash
?>
Output
\(abc\)\{10\}
6. Sub-storage
<?php
echo "<pre>";
// Sub-store usage
$date="[2012-08-09],[2012,09-19],[2011/08,09],[2012/10/09],[2013,08,01]";
// Match the legal dates in the above string
preg_match_all("/\[[0-9]{4}([\-,\/])[0-9]{2}\\1[0-9]{2}\]/",$date,$a);
print_r($a);
?>
Output
Array
(
[0] = > Array
(
[0] = > [2012-08-09]
[1] = > [2012/10/09]
[2] = > [2013,08,01]
)
[1] = > Array
(
[0] = > -
[1] = > /
[2] = > ,
)
)
For a detailed version, please refer to://www.ofstack.com/article/160947. htm
PS: Here are two very convenient regular expression tools for your reference:
JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript
Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg
For more readers interested in PHP related content, please check out the topics on this site: "php Regular Expression Usage Summary", "php Programming Security Tutorial", "php Security Filtering Skills Summary", "PHP Array (Array) Operation Skills Encyclopedia", "PHP Basic Syntax Introduction Tutorial", "php String (string) Usage Summary" and "php+mysql Database Operation Introduction Tutorial"
I hope this article is helpful to everyone's PHP programming.