Replacing

Started by chinmay.sahoo, 01-14-2017, 06:27:09

Previous topic - Next topic

chinmay.sahooTopic starter

The ereg_replace( ) function takes a pattern, a replacement string, and a string in which to search. It returns a copy of the search string, with text that matched the pattern replaced with the replacement string:

$changed = ereg_replace(pattern, replacement, string);

If the pattern has any grouped subpatterns, the matches are accessible by putting the characters \1 through \9 in the replacement string. For example, we can use ereg_replace( ) to replace characters wrapped with and tags with equivalent HTML tags:

$string = 'It is [b]not[/b] a matter of diplomacy.';
echo ereg_replace ('\[b]([^]]*)\[/b]', '<b>\1</b>', $string);
It is <b>not</b> a matter of diplomacy.



The eregi_replace( ) function is a case-insensitive form of ereg_replace( ). Its arguments and return values are the same as those for ereg_replace( ).