PHP Regular expression matches a specified label in a string _php tutorial

Source: Internet
Author: User
Tags php regular expression
Below we give a PHP regular expression matching string in the specified tag implementation program code, there is a need to learn friends can refer to this tutorial.

In PHP applications, regular expressions are mainly used to:

• Regular match: matches the corresponding content according to the regular expression
• Regular substitution: matches content according to regular expression and replaces
• Regular segmentation: Splitting strings based on regular expressions

Common

Preg_match_all ()
The Preg_match_all () function is used to perform a regular expression global match, which returns the number of times the entire pattern match succeeds (possibly 0), and returns FALSE if an error occurs.

Grammar:

int Preg_match_all (string pattern, string subject, array matches [, int flags])


Instance

$STR = "The two broad of the Gan Jia Kou East # label # 2 #标签3. #标签4, all hair $1234¥XC,CVM, ¥12,dflksjf How to # tags 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 matching Chinese characters are slightly different depending on the page encoding:

gbk/gb2312 Code: [x80-xff>]+ or [xa1-xff]+
utf-8 code: [x{4e00}-x{9fa5}]+/u
Example:

The code is as follows Copy Code

$STR = "Learning php is a happy thing. ";
Preg_match_all ("/[x80-xff]+/", $str, $match);
UTF-8 using:
Preg_match_all ("/[x{4e00}-x{9fa5}]+/u", $str, $match);
Print_r ($match);
?>

Additional notes:

Double-byte character encoding range

1. GBK (gb2312/gb18030)
X00-xff GBK Double byte encoding range
x20-x7f ASCII
Xa1-xff Chinese gb2312
X80-xff Chinese GBK

2. UTF-8 (Unicode)

U4E00-U9FA5 (English)
x3130-x318f (Korean
XAC00-XD7A3 (Korean)
u0800-u4e00 (Japanese)

http://www.bkjia.com/PHPjc/631584.html www.bkjia.com true http://www.bkjia.com/PHPjc/631584.html techarticle below we give a PHP regular expression matching string in the specified tag implementation program code, there is a need to learn friends can refer to this tutorial. In PHP applications, regular expressions are mainly ...

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