Menu

PHP str_replace(): Replace Text in a String

str_replace($search, $replace, $subject) replaces every occurrence of $search with $replace. Learn arrays of search and replace values, counting replacements, case-insensitive str_ireplace, and replacing only the first occurrence.

This page includes runnable editors - edit, run, and see output instantly.

str_replace($search, $replace, $subject) returns a copy of $subject with every occurrence of $search replaced by $replace. str_replace('world', 'PHP', 'Hello world') returns "Hello PHP".

The function returns the new string; it does not modify the variable you pass in. Assign the result, even back to the same variable: $text = str_replace(...).

Syntax

str_replace(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array

Each of the first three arguments can be a string or an array. If $subject is an array, the replacement runs on every element and you get an array back with the same keys: str_replace('.txt', '.md', ['a.txt', 'b.txt']) returns ['a.md', 'b.md']. The search is case-sensitive and matches plain text, not patterns. For patterns use preg_replace().

Replace several strings at once

Pass arrays for $search and $replace. The first search value is replaced by the first replacement, the second by the second, and so on. If $replace is a single string, every search value is replaced by it, which is the short way to remove a set of characters.

The order trap and strtr()

With arrays, str_replace() runs one replacement after another over the whole string. A later search can match text that an earlier replacement just inserted. strtr() with an array replaces everything in a single pass, tries the longest keys first, and never touches text it has already replaced.

Use strtr() for swaps and for template placeholders that might appear in the values.

Count the replacements

The fourth argument is filled with the number of replacements made. Pass a variable; PHP writes into it.

Case-insensitive replace with str_ireplace

str_ireplace() takes the same arguments and ignores case when matching. The replacement is inserted exactly as you wrote it.

str_ireplace() only folds ASCII letters. For case-insensitive replacement of accented or Cyrillic letters, use preg_replace() with the i and u modifiers.

Replace only the first occurrence

str_replace() always replaces everything. To replace just the first match, find it with strpos() and cut it out with substr_replace():

For the last occurrence, use strrpos() in place of strpos().

Remove spaces, newlines and other characters

Replacing with an empty string removes the text. A common clean-up removes line breaks or all spaces, and Japanese input often contains full-width spaces ( , U+3000) that a normal " " search does not match.

Put "\r\n" before "\n" and "\r" in the array, so a Windows line break becomes one space and not two.

Frequently Asked Questions

How do I replace text in a string in PHP?

Use str_replace($search, $replace, $subject): str_replace('cat', 'dog', 'cat and cat') returns "dog and dog". It replaces every occurrence and returns a new string; the original is not changed.

Is str_replace case-sensitive?

Yes. str_replace('php', 'PHP', 'Php') changes nothing. Use str_ireplace(), which takes the same arguments and ignores case.

How do I replace only the first occurrence in PHP?

str_replace() has no limit argument. Find the position with strpos() and use substr_replace($s, $replace, $pos, strlen($search)), or use preg_replace('/' . preg_quote($search, '/') . '/', $replace, $s, 1).

How do I replace multiple strings at once in PHP?

Pass arrays: str_replace(['a', 'b'], ['1', '2'], $s). The replacements run in order, so a later search can match text an earlier one inserted. strtr($s, ['a' => '1', 'b' => '2']) replaces everything in one pass and never re-replaces.

Does str_replace work with Japanese text?

Yes. str_replace() matches bytes, and a complete UTF-8 character never matches inside another character, so str_replace('東京', '大阪', $s) is safe. There is no need for an mb_ version.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED