The PHP regular expression matches the specified tag in the string.

Source: Internet
Author: User
Tags php regular expression regular expression

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)

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.