,

PHP:Str replace

Contents

File:PHP_logo.png

str_replace() is case-sensitive. (See example.) Use str_ireplace() for insensitive case work.

Case-sensitive

echo str_replace(" a "," one ","A bright a clue for your test");

Output:

A bright one clue for your test

Replace Multiple Characters (Arrays)

Use array to substitute many different characters in the same string:

echo str_replace(array(" a "," clue ")," one ","A bright a clue for your test");

Output:

A bright one one for your test

OR, more useful, you can use TWO arrays:

echo str_replace(array(" a "," clue "),array(" one "," tip "),"A bright a clue for your test");

Output:

A bright one tip for your test


PHP function

Replace characters in a string

str_replace(to_find,to_replace,main_string,counter)
to_find
Characters to find and erase
to_replace
Characters to use to replace by the erased ones
main_string
String that the replacement will take place
counter
Count how many time the replacement happened in that main string