(From: http://blog.csdn.net/harryxlb/article/details/7344471)
Application of Regular Expressions in PHP
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.
There are two types of Regular Expression functions in PHP: Perl-Compatible Regular Expression functions and POSIX-extended regular expression functions. There is little difference between the two, and Perl is recommended to be compatible with regular expression functions. Therefore, the following uses Perl Compatible Regular Expression functions as an example.
Delimiters
Regular Expression functions in Perl compatibility mode. The regular expression must be written in the delimiters. Any character that is not a letter, number, or backslash () can be used as a separator. We usually use/as a delimiter. For more information, see the following example.
Prompt
Although regular expressions are very powerful, do not use regular expression functions if common string processing functions can be completed, because regular expressions are much less efficient. For common string processing functions, see PHP string processing.
Preg_match ()
The preg_match () function is used for regular expression matching. If the regular expression matches successfully, 1 is returned. Otherwise, 0 is returned.
Syntax:
int preg_match( string pattern, string subject [, array matches ] )
Parameter description:
Parameters |
Description |
Pattern |
Regular Expression |
Subject |
Objects to be retrieved |
Matches |
(Optional) an array storing the matching results. $ matches [0] will contain the text that matches the entire pattern, $ matches [1] will contain the text that matches the child pattern in the first captured bracket, and so on |
Example 1:
<?phpif(preg_match("/php/i", "PHP is the web scripting language of choice.", $matches)){ print "A match was found:". $matches[0];} else { print "A match was not found.";}?>
Browser output:
A match was found: PHP
In this example, the I modifier is used to match PHP in text without case sensitivity.
Prompt
Preg_match (): After the first successful match, the match will stop. If you want to match all the results, that is, find the end of the subject, you need to use the preg_match_all () function.
Example 2: Obtain the host domain name from a URL:
<? PHP // obtain the host name preg_match ("/^ (http: //) from the URL ://)? ([^/] +)/I "," http://www.5idev.com/index.html ", $ matches); $ host = $ matches [2]; // obtain the preg_match ("/[^. /] +. [^. /] + $/", $ host, $ matches); echo" Domain Name: {$ matches [0]} ";?>
Browser output:
Domain Name: 5idev.com
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 ] )
Parameter description:
Parameters |
Description |
Pattern |
Regular Expression |
Subject |
Objects to be retrieved |
Matches |
Array storing matching results |
Flags |
Optional. Specify the order in which the matching results are placed in matches. The available tags are:
- Preg_pattern_order: default value. Sort the result to $ matches [0] as an array of all pattern matching. $ matches [1] is an array consisting of strings matched by the Child pattern in the first bracket, and so on.
- Preg_set_order: sort the result to $ matches [0] As the array of the first matching item, $ matches [1] As the array of the second matching item, and so on.
- Preg_offset_capture: If this tag is set, the offset of the affiliated string is also returned for each matching result.
|
The following example shows that the keywords (PHP) in all <PRE> </PRE> labels in the text are displayed in red.
<? PHP $ STR = "<PRE> learning PHP is a pleasure. </PRE> <PRE> All phper needs to work together! </PRE> "; $ kW =" php "; preg_match_all ('/<PRE> ([ss] *?) </PRE>/', $ STR, $ mat); For ($ I = 0; $ I <count ($ mat [0]); $ I ++) {$ mat [0] [$ I] = $ mat [1] [$ I]; $ mat [0] [$ I] = str_replace ($ kW, '<span style = "color: # ff0000"> '. $ kW. '</span>', $ mat [0] [$ I]); $ STR = str_replace ($ mat [1] [$ I], $ mat [0] [$ I], $ Str);} echo $ STR;?>
Regular Expression matching Chinese Characters
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:
<? 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);?>
Output:
Array ([0] => array ([0] => learning [1] => is a pleasure. ))