str_replace

Replace all occurrences of the search string with the replacement string

Syntax

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

Parameters

search

The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

replace

The replacement value that replaces found search values. An array may be used to designate multiple replacements.

subject

The string or array being searched and replaced on, otherwise known as the haystack. If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

count

If passed, this will be set to the number of replacements performed.

Return

This function returns a string or an array with the replaced values.

Examples

1 · search replace subject

<?

$search = "case";
$replace = "replace";
$subject = "case Case CASE";

$return = str_replace($search, $replace, $subject);

echo $return;

?>
replace Case CASE

2 · count

<?

$search = "case";
$replace = "replace";
$subject = "case Case CASE";

$return = str_replace($search, $replace, $subject, $count);

echo $count;

?>
1

3 · 1

<?

$search = array("search1", "search2", "search3");
$replace = "replace";
$subject = "search1 search2 search3";

$return = str_replace($search, $replace, $subject);

echo $return;

?>
replace replace replace

4 · 2

<?

$search = array("search1", "search2", "search3");
$replace = array("replace1", "replace2", "replace3");
$subject = "search1 search2 search3";

$return = str_replace($search, $replace, $subject);

echo $return;

?>
replace1 replace2 replace3

5 · 3

<?

$search = array("\r\n", "\n", "\r"); // \r\n first
$replace = "<br>";
$subject = "line\nline\rline\r\nline";

$return = str_replace($search, $replace, $subject);

echo $return;

?>
line<br>line<br>line<br>line

6 · 4

<?

$search = array('a', 'b', 'c', 'd', 'e');
$replace = array('b', 'c', 'd', 'e', 'f');
$subject = 'a';

$return = str_replace($search, $replace, $subject);

echo $return;

?>
f

7 · 5

<?

$search = array('a', 'p');
$replace = array('apple', 'pear');
$subject = 'apricot';

$return = str_replace($search, $replace, $subject);

echo $return;

?>
apearpearlepearricot

str_ireplace

addcslashes

addslashes

bin2hex

chop

chr

chunk_split

convert_uudecode

convert_uuencode

count_chars

crc32

crypt

echo

explode

fprintf

get_html_translation_table

hebrev

hebrevc

hex2bin

html_entity_decode

htmlentities

htmlspecialchars

htmlspecialchars_decode

implode

join

lcfirst

levenshtein

localeconv

ltrim

md5

md5_file

metaphone

nl_langinfo

nl2br

number_format

ord

parse_str

print

printf

quoted_printable_decode

quoted_printable_encode

quotemeta

rtrim

setlocale

sha1

sha1_file

similar_text

soundex

sprintf

sscanf

str_contains

str_ends_with

str_getcsv

str_ireplace

str_pad

str_repeat

str_rot13

str_shuffle

str_split

str_starts_with

str_word_count

strcasecmp

strchr

strcmp

strcoll

strcspn

strip_tags

stripcslashes

stripos

stripslashes

stristr

strlen

strnatcasecmp

strnatcmp

strncasecmp

strncmp

strpbrk

strpos

strrchr

strrev

strripos

strrpos

strspn

strstr

strtok

strtolower

strtoupper

strtr

substr

substr_compare

substr_count

substr_replace

trim

ucfirst

ucwords

vfprintf

vprintf

vsprintf

wordwrap

PHP

Home Menu