PHP » Strings » ereg_replace()

Syntax:
string ereg_replace(string pattern, string replacement, string s)
pattern
Pattern to be replaced.
replacement
Replacement string.
s
String to make replacements in.

Makes replacements using regular expressions.

This function has been deprecated as of PHP 5.3.0.

ereg_replace() replaces the occurrences of the pattern with the replacement string. The resulting string is returned. Submatches can be accessed in the replacement with backslashes.

Examples

Code:
<pre>
<?php

$s = "Coding PHP is fun.";

$pattern = "(.*)PHP(.*)";
$replacement = " They say \\1other languages\\2";

print ereg_replace($pattern, $replacement, $s);

?>
</pre>
Output:
They say Coding other languages is fun.
Explanation:

"PHP" is replaced with "other languages", and the sentence is changed a little, using \1 and \2 to access the parts within parentheses.

See Also: