Log in / create account | Login with OpenID
DocForge
An Open Wiki For Software Developers

PHP/str replace

From DocForge

< PHP

str_replace will return a string, or an array of strings, with all occurrences of one string replaced with another.

str_replace($search_for, $replace_with, $subject [, $count])

$search_for can be a single string or array of strings to search for.

$replace_with can be a single string or array of strings to use as replacements. If a string is passed, but $search_for is an array, this single string is used to replace any values found. If an array is passed of equal length to a $search_for array, each string found in the search array is replaced with the corresponding string in the replacement array. If the replacement array is shorter than the search array, empty strings are used for the remaining replacement values.

$subject is a string, or array of strings, being searched.

$count limits the replacements to stop after this many are found.

The return value is a string if the $subject is a string. The return value is an array if the $subject is an array.

For direct string replacements str_replace is faster than preg_replace.

See Also [edit]