PHP Small experience: analytic Preg_match and Preg_match_all function _php techniques

Source: Internet
Author: User
The application of regular expression in PHP
in PHP applications, regular expressions are used primarily for:
• Regular match: matches the corresponding content according to the regular expression
• Regular substitution: matching content based on regular expressions and replacing
• Regular segmentation: Splitting strings based on regular expressions
There are two types of regular expression functions in PHP, one is Perl-compatible regular expression functions, the other is 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.
delimiters
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.
Tips
Although the regular expression is very powerful, if you can do it with a normal string handler, try not to use regular expression functions, because regular expressions are much less efficient. About normal string processing functions.
Preg_match ()
The Preg_match () function is used to match a regular expression, successfully returning 1, otherwise returning 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];
To get the following two segments from the host name
Preg_match ("/[^./]+.[ ^./]+$/", $host, $matches);
echo "domain name: {$matches [0]}";
?>

Browser Output:
Copy Code code as follows:

The domain name is: jb51.net

Preg_match_all ()
The 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:
Parameters Description
Pattern Regular expressions
Subject Objects that need to match the retrieved
Matches An array that stores matching results
Flags

Optionally, specify the order in which the matching results are placed in the matches, and the optional tags are:

  1. Preg_pattern_order: By default, the result is sorted so that $matches [0] matches all patterns, $matches [1] is an array of strings that match the child pattern in the first bracket, and so on
  2. Preg_set_order: Sort results by making $matches [0] An array of the first set of matches, $matches [1] as an array of the second set of matches, and so on
  3. Preg_offset_capture: If you set this tag, the matching result for each occurrence also returns its attached string 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.
)

)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.