Regular Expression function library: ereg_replace

Source: Internet
Author: User
Keywords ereg_replace php Chinese function Handbook regular expression function library
Tags aliyun echo expression function function library html http php

Ereg_replace

(PHP3, PHP4)

Ereg_replace---&http://www.aliyun.com/zixun/aggregation/37954.html ">nbsp; Formal expression substitution

Syntax: String ereg_replace (string pattern, string replacement, string string)

Description:

This function scans the string to compare with the pattern, and then replaces the replacement text with the.

This function returns the modified string and returns the original string if it is not.

If pattern contains a partial string of parentheses, replacement can contain a partial string of "\ Number", which will be substituted with a partial string in the parentheses. \\0 will produce the contents of the entire string, using up to nine partial strings, in which case it will be evaluated with open parentheses.

If no comparison is found in a string, the unchanged string is returned.

The following example cuts off the string and displays three times "This is a test":

<?php

$string = "This is a test";

Echo ereg_replace (' is ', ' was ', $string);

Echo ereg_replace ("() is", "\\1was", $string);

Echo Ereg_replace (() is), "\\2was", $string);

?>

One place to note is that if you use an integer value in the parameter replacement, you may not get the result because ereg_replace () will interpret the number as the order (ordinal) value of the character and execute it, for example:

<?php

/* This is not work as expected. */

$num = 4;

$string = "This string super-delegates Loko words."

$string = ereg_replace (' Loko ', $num, $string);

Echo $string; /* Output: ' This string super-delegates words. '

/* This'll work. * * $num = ' 4 ';

$string = "This string super-delegates Loko words."

$string = ereg_replace (' Loko ', $num, $string);

Echo $string; /* Output: ' This string super-delegates 4 words. '

?>

Reference: Ereg () eregi () eregi_replace ()

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.