Regular expressions are applied in PHP
in PHP applications, regular expressions are used primarily for:
• Regular matches: matching the corresponding content according to the regular expression
· Regular substitution: matching content based on regular expressions and replacing
• Regular segmentation: dividing strings based on regular expressions
There are two types of regular expression functions in PHP, one for Perl-compatible regular expression functions and one for POSIX extended regular expression functions. There is little difference, and Perl-compatible regular expression functions are recommended, so the following are examples of Perl-compatible regular expression functions.
delimiter
a regular expression function in Perl compatibility mode whose regular expression needs to be written in a delimiter. Any character that is not a letter, number, or backslash () can be used as a delimiter, and usually we use/as a delimiter. See the example below for specific use.
Tip
Although regular expressions are powerful, you should try not to use regular expression functions if you can do it with normal string handlers, because regular expressions are much less efficient. About normal string processing functions. The
preg_match ()
Preg_match () function is used to match a regular expression, returning 1 successfully, or 0.
Syntax:
int Preg_match (string pattern, string subject [, array matches])
parameter description:
Parameters |
Description |
Pattern |
Regular expressions |
Subject |
Objects that need to match the retrieved |
Matches |
Optionally, stores an array of matching results, $matches [0] will contain text that matches the entire pattern, $matches [1] will contain text that matches the child pattern in the first captured bracket, and so on |
Example 1:
Copy Code code as follows:
<?php
if (Preg_match ("/php/i", "PHP is the Web scripting language of choice.", $matches)) {
print "A match was found:". $matches [0];
} else {
print "A match is not found.";
}
?>
Browser Output:
Copy Code code as follows:
A match was found:php
In this example, because the I modifier is used, it is case-insensitive to match PHP in the text.
Tips
Preg_match () stops the match after the first match succeeds, and the Preg_match_all () function is used if you want to achieve a match for all the results, that is, to the end of the subject.
Example 2, get the host domain name from a URL:
Copy Code code as follows:
<?php
//Get host name from URL
Preg_match ("/^" (http://)? [^/]+)/I "," http://www.jb51.net/index.html ", $matches);
$host = $matches [2];
//From the host name to obtain the following two segment
Preg_match ("/[^./]+.[ ^./]+$/", $host, $matches);
echo "domain name: {$matches [0]}";
?>
Browser Output:
Copy Code code as follows:
domain name is: jb51.net
The
Preg_match_all ()
Preg_match_all () function is used to make a regular expression global match, successfully returning the number of times the entire pattern match (possibly 0), or FALSE if the error returns.
Syntax:
int Preg_match_all (string pattern, string subject, array matches [, int flags])
parameter description:
parameter |
description |
pattern |
regular expression |
subject |
needs to match the retrieved object |
matches |
store an array of matching results |
flags |
optional, specify the order in which the matching results are placed in the matches, and the available tags are:
- preg_pattern_order: By default, the result is sorted so that $matches [0] matches the full pattern, $matches [1] An array of strings that match the child pattern in the first bracket, and so on
- preg_set_order: Sort results so that $matches [0] is an array of the first set of matches, $matches [1] is an array of the second set of matches, and so on
- preg_offset_capture: If this tag is set, the matching result for each occurrence is also returned to its subordinateString offset
|
The following example shows the keyword (PHP) in all <pre></pre> tags in the text to be displayed in red.
Copy Code code as follows:
<?php
$str = "<pre> learning PHP is a happy thing." </pre><pre> all the phper need 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 matching Chinese characters
regular matching Chinese characters vary slightly according to page encoding:
gbk/gb2312 Encoding: [x80-xff>]+ or [xa1-xff]+
utf-8 encoding: [x{4e00}-x{9fa5}]+/u
Example:
Copy Code code as follows:
<?php
$str = "Learning php is a happy thing." ";
Preg_match_all ("/[x80-xff]+/", $str, $match);
//utf-8 use:
//preg_match_all ("/[x{4e00}-x{9fa5}]+/u", $str, $match);
Print_r ($match);
?>
output:
Copy Code code as follows:
Array
(
[0] => Array
(
[0] => study
[1] => is a happy thing.
)
)