PHP string regular expression replacement function preg_replace_PHP tutorial

Source: Internet
Author: User
Tags ereg
PHP string regular expression replacement function preg_replace. Syntax: mixedpreg_replace (mixedpattern, mixedreplacement, mixedsubject); return value: mixed type data function type: data processing content description: This function uses pattern rules to parse syntax: mixed preg_replace (mixed pattern, Tern, mixed replacement, mixed subject );

Returned value: Hybrid Data

Function type: data processing

Description: This function uses the pattern rule to parse and compare the string subject. the string to be replaced is the parameter replacement. The returned value is a string of the mixed type.

1. preg_replace ()

$ Msg = preg_replace ("//Is "," ", $ msg); ----- deleteAnd the middle part
$ Msg = preg_replace ("/<[^>] +>/", "", $ msg); ----- delete <> and intermediate content

I (PCRE_CASELESS)
If this modifier is set, characters in the mode match both uppercase and lowercase letters.
S (PCRE_DOTALL)
If this modifier is set, the DOT metacharacters (.) in the pattern match all characters, including line breaks. If this parameter is not set, line breaks are not included. This is equivalent to the/s modifier of Perl. For example, [^ a] always matches a line break, regardless of whether this modifier is set.


2. ereg () and eregi ()

Note: The preg_match () function is usually a faster alternative than ereg ().

Eregi (" ] +)> (. +)", $ Data, $ B) ---- check whether the $ data contains the body tag. If yes, assign $ B [0] to the parameter and $ B [1] to the intermediate part.

Bool ereg (string pattern, string [, array regs])

Int eregi (string pattern, string, array [regs])

Eregi () and ereg () are similar in usage. The difference is that ereg () is case sensitive, and eregi () is case-insensitive.

// Comparison of preg_replace () and ereg_replace () functions
// ------- Preg_replace ()--------------------------
// 1. replace string search
$ Str = "daoyu shi ge hao hai zi5555 ";
$ Pattern = "/s/"; // an error occurs if the variable is defined as $ pattern_1.
$ Str = preg_replace ($ pattern, '-', $ str );
Echo $ str ."
";

// 2. reverse reference of the string
// Method 1
$ Pat = "/(w +)-(d +) /I ";
$ Str = preg_replace ($ pat, "$1", $ str );
Echo $ str ."
";

// Note: If it is in the following format, you will find that the match is: zi-so you can think that in the case of {6} of the number of times, he ($1) match the last time
$ Pat = "/(w +)-) {6} (d +)/I ";
$ Str = preg_replace ($ pat, "$1", $ str );
Echo $ str ."
";


// Method 2
$ Str = "daoyu-shi-ge-hao-hai-zi-5555 ";
$ Pat = "/(w +)-(d +) /I ";
$ Str = preg_replace ($ pat, "1", $ str );
Echo $ str ."
";

// Note: When the regular expression is $ pat = "/(w +)-) {6} (d +)/I"; it is the same as the above

Required mixed preg_replace (mixed pattern, mixed replacement, mixed subject); return value: mixed type data function type: data processing content description: This function is implemented by pattern rules...

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.