PHP Small experience: parsing preg

Source: Internet
Author: User
Tags preg regular expression

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.
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.
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.
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:


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.
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:


//Get host name from URL

Preg_match ("/^" (http://)? [^/]+)/I "," ", $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:


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.
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

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

  1. 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
  2. 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
  3. 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:


$str = "&lt;pre&gt; learning PHP is a happy thing." &lt;/pre&gt;&lt;pre&gt; all the phper need to work together! &lt;/pre&gt; ";

$kw = "PHP";

Preg_match_all ('/&lt;pre&gt; (ss]*?) &lt;/pre&gt;/', $str, $mat);

for ($i =0; $i &lt;count ($mat [0]); $i + +) {

$mat [0][$i] = $mat [1][$i];

$mat [0][$i] = str_replace ($kw, ' &lt;span style= ' color: #ff0000 ' &gt; '. $kw. ' &lt;/span&gt; ', $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

Copy Code code as follows:


$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);



Copy Code code as follows:

[0] => Array
[0] => study
[1] => is a happy thing.


Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.