Sometimes it is necessary to match in some specific business scenarios, or to extract some key information, such as matching some links in a Web page,
When extracting some data, a regular match may be used.
Here are some of the most common regular processing functions in PHP.
I. Preg_replace ($pattern, $replacement, $subject)
Performs a search and replace of a regular expression.
<?php echo "<pre>"; $str = "12,34:56;784;35,67:897:65"; Require the above:,; change to a space Print_r (preg_replace ("/[,;:]/", "", $str)); >//Output 12 34 56 784 35 67 897 65
Ii. Preg_match ($pattern, $subject,& $matches)
Perform a matching regular expression
<?php echo "<pre>"; $str = "<a href=\" https://www.baidu.com\ "> Group purchase goods </a>"; Match out the link address preg_match ("/<a href=\" (. *?) \ ">.*?<\/a>/", $str, $res); Print_r ($res);? >//output Array ( [0] = buy goods [1] = https://www.baidu.com)
Iii. Preg_match_all ($pattern, $subject,& $matches)
Performs a global regular expression match
<?php echo "<pre>"; $str =<<<eof <div> <a href= "index.php" > Home </a> <a href= "category.php?id= 3 ">gsm phone </a> <a href=" category.php?id=4 "> Dual mode phone </a> <a href=" category.php?id=6 " > Mobile Accessories </a> </div>EOF; Use global regular match preg_match_all ("/<a href=\" (. *?) \ "> (. *?) <\/a>/s ", $str, $res); Print_r ($res);? >//output Array ( [0] = = Array ( [0] ~ = Home [1] = + GSM mobile phone [2] + + dual mode mobile phone [3] = = 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 [1] = > GSM mobile phone [2] = + Dual mode mobile phone [3] = = Mobile Phone accessories ))
Iv. preg_split ($pattern, $subject)
Separating strings with a regular expression
<?php echo "<pre>"; $str = "12,34:56;784;35,67:897:65"; Delimited string $arr = Preg_split ("/[,;:]/", $str); Print_r ($arr);? >//output Array ( [0] = [ 1] = [ 2] = [ 3] = + 784 [4] = [5] = 67
[6] = 897 [7] = 65
)
Wu, Preg_quote ($STR)
Escaping regular expression characters
The regular expression special characters are:. \ + * ? [ ^ ] $ ( ) { } = ! < >:-
<?php echo "<pre>"; Echo Preg_quote ("(ABC) {10}");//Adds a backslash before the character of each regular expression syntax? >//output \ (abc\) \{10\}
Six, sub-storage
<?php echo "<pre>"; Sub-storage uses $date = "[2012-08-09],[2012,09-19],[2011/08,09],[2012/10/09],[2013,08,01]"; Match the valid date in the string above 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 detailed version, please refer to: https://www.cnblogs.com/yafei236/p/4168290.html
The Pcre function in PHP, the regular expression handler function.