In PHP applications, regular expressions are mainly used:
• Regular Expression Matching: matches the corresponding content according to the regular expression.
• Regular expression replacement: Match and replace the content according to the regular expression.
• Regular expression Segmentation: Splits strings based on regular expressions.
Common
Preg_match_all ()
The preg_match_all () function is used to perform global matching of regular expressions. If the regular expression is successfully matched, the number of times (which may be zero) is returned. If an error occurs, FALSE is returned.
Syntax:
Int preg_match_all (string pattern, string subject, array matches [, int flags])
Instance
$ Str = "2. Wide Ganjiakou East # label 1 # label 2 # label 3. # Tag 4: $1234 $ xc, cvm, ¥12, dflksjf # tag 5.x
The code is as follows: |
Copy code |
# Tag6.cvxcv ""; Preg_match_all ('/# ([a-zA-Z0-9x7f-xff] +)/', $ str, $ mat ); Print_r ($ mat ); Preg_match ("/[x {00a5} x {ffe5}] (d +)/u", $ str, $ mat ); Print_r ($ mat ); |
Regular Expression Matching Chinese characters are slightly different based on the page encoding:
• GBK/GB2312: [x80-xff>] + or [xa1-xff] +
• UTF-8 code: [x {4e00}-x {9fa5}] +/u
Example:
The code is as follows: |
Copy code |
<? Php $ Str = "learning php is a pleasure. "; Preg_match_all ("/[x80-xff] +/", $ str, $ match ); // UTF-8 use: // Preg_match_all ("/[x {4e00}-x {9fa5}] +/u", $ str, $ match ); Print_r ($ match ); ?> |
Note:
Double byte character encoding range
1. GBK (GB2312/GB18030)
X00-xff GBK dubyte encoding range
X20-x7f (ASCII)
Xa1-xff Chinese gb2312
X80-xff Chinese gbk
2. UTF-8 (Unicode)
U4e00-u9fa5)
X3130-x318F (Korean
XAC00-xD7A3 (Korean)
U0800-u4e00 (Japanese)