Table of Contents

str_replace

WMMEGA FW >= 2.1188

Replace all occurrences of the search string with the replacement string

Description

string str_replace ( string $search, string $replace, string $subject [, int &$count ])

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

Parameters

$search: The string to search for

$replace: The string to replace with

$subject: The string with the search strings to be replaced

&$count: [Optional] A pointer to an int which will contain the number of replacements made. Make sure you pass this with a & before the variable (pass by reference)

Return Values

string: A string based on $subject with all the search strings replaced by their replacement strings.

Example

<?
$str="Hello world, you are the nicest world that I know.";
 
$result=str_replace("world", "chick", $str);
$result=str_replace("Hello", "Goodbye", $result);
print($result);
?>

Example 2

<? 
 
$string1="This is a fine time to eat.  It really is.";
$cnt=0;
$str=str_replace("is","",$string1,&$cnt);
print($str);
print("\r\nReplaced ".$cnt." times");  
?>

Output is:

Th  a fine time to eat.  It really .
Replaced 3 times

See Also

charat() - Return the ASCII code for a character in a string at an index

strlen() - Return the length of a string

strrpos() - Return the position of the last occurrence of a needle in a haystack

strtolower() - Return the lowercase version of a string

strtoupper() - Return the UPPERCASE version of a string

substr() - Return part of a string

ucfirst() - Convert a string to lowercase except for the first character