Home > Backend Development > PHP Tutorial > php ereg_replace function

php ereg_replace function

高洛峰
Release: 2016-11-29 16:08:42
Original
1203 people have browsed it

$string = "this is a test";

echo str_replace(" is", "was", $string);

echo ereg_replace("( )is", "1was", $string); //where 1 is the space in the first bracket

echo ereg_replace("(( )is)", "2was", $string); //2 is the space in the second bracket, and the above three lines are the "is" Replace with "was"; both have spaces.

ereg_replace (string pattern, string replacement, string string)

That is to say, if pattern contains a string with () (for example; there are spaces in brackets), then your replacement can use a string such as 1, Then this 1 can be replaced with the string in your first bracket. If it is 2, replace it with the string in the second bracket.

The brackets are from left to right, whichever is the left bracket. The one below It is to remove the string comparison analysis and replacement such as &page=1 in the url.

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

Return value: string

Function type: data processing

Content description: This function uses pattern rules to parse and compare string strings. The string to be replaced is the parameter replacement, and the return value is the string type, which is the replaced string result.

Usage example, ken@freebsdrocks. com on 16-mar-1999, the code is as follows:

$text = 'this is a {1} day, not {2} and {3}.';

$daytype = array( 1 => 'fine',

2 => 'overcast',

3 => 'rainy' );

while (ereg ('{([0-9] +)}', $text, $regs)) {

$found = $regs[1];

$text = ereg_replace("{".$found."}", $daytype[$found], $text);

}

echo "$textn";

// this is a fine day, not overcast and rainy.

?>

ken@freebsdrocks.com and also proposed a perl program example with the same function as follows:

$text = 'this is a {1} day, not {2} and {3}.';

%daytype = ( 1 => 'fine',

2 => 'overcast',

3 => 'rainy' );

$text =~ s/{(d+)}/$daytype{$1}/eg;

print "$textn";


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template