›Php ereg_replace() function
What is ereg_replace() function? This function scans string for matches to pattern, then replaces the matched text with replacement, this tutorial is well explained with 9 Examples.
Php ereg_replace() Function
The ereg_replace() function searches for string specified by pattern and replaces pattern with replacement if found. The ereg_replace() function operates under the same premises as ereg(), except that the functionality is extended to finding and replacing pattern instead of simply locating it.
Syntax
string ereg_replace ( string $pattern , string $replacement , string $string )
pattern: A POSIX extended regular expression.
replacement: If pattern contains parenthesized substrings, replacement may contain substrings of the form \\digit, which will be replaced by the text matching the digit’th parenthesized substring; \\0 will produce the entire contents of string. Up to nine substrings may be used. Parentheses may be nested, in which case they are counted by the opening parenthesis.
string:The input string.
Example 1
Simple String Replacement, Replacing “five” with “ten” in “I have five mangoes” String:
echo ereg_replace("five", "ten", "I have five mangoes");
Output
From: I have five mangoes To: I have ten mangoes
Example 2
Simple String Replacement, Replacing “to Buy Mangoes” with “Bananas” in “I like Bananas!” String:
$string = "I like Bananas";
echo ereg_replace("Bananas", "to Buy Mangoes", $string);
Output
From: I like Bananas! To: I like to Buy Mangoes!
Example 3
Simple String Replacement, Replacing “Windows” with “Mac Ox” in “I like Mac Ox!” String:
$string = "I like Mac Ox!"; $pattern = "Mac Ox"; $replacement = "Windows"; echo ereg_replace($pattern, $replacement, $string);
Output
From: I like Mac Ox! To: I like Windows!
Example 4
Simple String Replacement, Replacing Spaces between words with Dashes(-) String:
$string = "Learn how to build your own website"; $pattern = " "; $replacement = "-"; echo ereg_replace($pattern, $replacement, $string);
Output
From: Learn how to build your own website. To: Learn-how-to-buld-your-own-website.
Example 5
Simple String Replacement, Removing a word from the string:
$string = "How to make money online"; $pattern = " online"; $replacement = ""; echo ereg_replace($pattern, $replacement, $string);
Output
From: How to make money online To: How to make money
Example 6
Simple String Replacement, highlighting a simple word:
$string = "Learn Php step by step here"; $pattern = "Php"; $replacement = "<font color=red>Php</font>"; echo ereg_replace($pattern, $replacement, $string);
Output
From: Learn Php step by step here
To: Learn Php step by step here
Example 7
Simple String Replacement, highlights the recognized patterns (a good tool for experiments) :
$string = "Learn Php step by step here"; $pattern = "Php"; $replacement = "<font color=red>\\0</font>"; echo ereg_replace($pattern, $replacement, $string);
Output
From: Learn Php step by step here
To: Learn Php step by step here
Example 8
Simple String Replacement, Make all url starts with “http://” clickable:
$string = "Do you want to learn php, please go to http://www.urphp.com";
echo ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $string);
Output
From: Do you want to learn php, please go to www.urphp.com
To: Do you want to learn php, please go to www.urphp.com
Example 9
Simple String Replacement, Make all url starts with “www” clickable:
$string = "Do you want to learn php, please go to www.urphp.com";
echo ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $string);
Output
From: Do you want to learn php, please go to www.urphp.com
To: Do you want to learn php, please go to www.urphp.com




