Regular Expression Function preg in PHP

Source: Internet
Author: User
Tags preg
Preg_match (); used for regular expression matching, and only match once preg_match_all (); used for regular expression matching, all compliant with the rule will be matched preg_replace (); regular Expression replacement function preg_splite (); Regular Expression division function preg_match (mode, stringsubject, arraymatches)

Preg_match (); // used for regular expression matching, and only matches preg_match_all () once; // used for regular expression matching, preg_replace () will be matched for all compliant rules; // The regular expression replacement function preg_splite (); // The regular expression division function preg_match (mode, string subject, arraymatches)

Preg_match (); // used for regular expression matching only once

Preg_match_all (); // used for regular expression matching. All matching rules are matched.

Preg_replace (); // Regular Expression replacement function

Preg_splite (); // Regular Expression division Function


Preg_match (mode, string subject, arraymatches)

Here, mode is a regular rule, string subject is the string to be matched, and arraymatches is the matching result array.

Example:

 

Output:
Array ( [0] => 9 ) 

Preg_match_all (mode, string subject, arraymatches)

This function is similar to the preg_match_all () function, but it matches all the required content and stores it in a string.


Example:

 

Output:

Array ( [0] => Array ( [0] => 8 [1] => 8 [2] => 9 [3] => 9 [4] => 9 ) ) 

Preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])

Use regular expressions to replace related content, similar to the previously learned str_replace string replacement, but the function is better than it.


Features: 1. The replacement content can be a regular or array regular

2. The replacement content can be replaced by the modifier e.

Purpose: replace some complicated content and use it for content conversion.

Example 1 -- array regular:

 Author: {author}
Url: {url} "; echo" "; If ($ tag = preg_replace ($ mode, $ re, $ str) {echo $ tag;} else {echo" cannot be replaced! ";}?>


Output:

Title: code cloud Author: qianshou address: http://codecloud.duapp.com/

Example 2 -- replace the ubb code:

 

Output:

Welcome to my blog: http://blog.csdn.net/qsyzb

Preg_split (string pattern, string subject [, intlimit [, int flags])

Regular expressions are used to cut related content. Similar to the previously learned explode cut function, explode only has limitations in one way.

Example:

 

Output:

Array(    [0] => one    [1] => two    [2] => three    [3] => four)

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.