PHP string Regular replacement function preg_replace_php Tutorial

Source: Internet
Author: User
Tags character classes ereg
Syntax: Mixed preg_replace (mixed pattern, mixed replacement, mixed subject);

Return value: Mixed type data

Function type: Data processing

Description: This function parses the subject string with the rule of pattern, and the string to be replaced is the parameter replacement. The return value is a mixed-type data, which is substituted for the string result.

1. Preg_replace ()

$msg = Preg_replace ("/ /is", "", $msg); -----Delete and the middle part
$msg = Preg_replace ("/<[^>]+>/", "", $msg); -----is to delete <> and intermediate content

I (pcre_caseless)
If this modifier is set, the characters in the pattern will match the uppercase and lowercase letters.
S (pcre_dotall)
If this modifier is set, the dot character (.) in the pattern matches all characters, including the line break. Without this setting, the line break is not included. This is equivalent to Perl's/s modifier. Exclude character classes such as [^a] always match line breaks, 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)----see if there is a body tag in the $data. If so, assign the parameter $b[0], and the middle part assigns a value of $b[1].

BOOL Ereg (string pattern, string string [, array regs])

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

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

Comparison of the use of the Preg_replace () and ereg_replace () functions
-------Preg_replace ()--------------------------
1. Replacing the search for a string
$str = "Daoyu shi ge hao hai zi 5555";
$pattern = "/s/"; If you define a variable as $pattern_1, an error occurs
$str = Preg_replace ($pattern, '-', $str);
echo $str. "
";

2. Inverse reference to a string
Method One
$pat = "/(w+)-(w+)-(w+)-(w+)-(w+)-(w+)-(d+)/I";
$str =preg_replace ($pat, "$", $str);
echo $str. "
";

Note: If it is the following form you will find the match is: zi-so you can think that in the case of a number of {6}, he ($) matches the last
$pat = "/((w+)-) {6} (d+)/I";
$str =preg_replace ($pat, "$", $str);
echo $str. "
";


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

Note: When the regular is written $pat= "/((w+)-) {6} (d+)/I";

http://www.bkjia.com/PHPjc/632276.html www.bkjia.com true http://www.bkjia.com/PHPjc/632276.html techarticle syntax: Mixed preg_replace (mixed pattern, mixed replacement, mixed subject); return value: Mixed type data function type: Data processing content Description: This function takes the rule of pattern To solve ...

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