Backend Development 5 min read

How to Use PHP str_replace() for Replacing Characters, Strings, and Arrays

This article explains PHP's str_replace() function, demonstrating how to replace single characters, whole strings, array elements, and perform multiple replacements, including case‑sensitive considerations, with clear code examples. It also compares str_ireplace() for case‑insensitive operations and provides links to further learning resources.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Use PHP str_replace() for Replacing Characters, Strings, and Arrays

The PHP str_replace() function is used to replace specified characters or strings within a string. Its usage is very flexible, allowing replacement of single characters, strings, arrays, and more.

Replace single character:

The first argument of str_replace() is the character to be replaced, the second argument is the new character, and the third argument is the target string.

<code>$str = "Hello World";
$new_str = str_replace(" ", "_", $str);
echo $new_str; // Output: Hello_World</code>

Replace string:

str_replace() can also be used to replace entire substrings. The principle is the same as replacing a single character.

<code>$str = "Hello world";
$new_str = str_replace("world", "PHP", $str);
echo $new_str; // Output: Hello PHP</code>

Replace array:

str_replace() can replace elements within an array. In this case, the first parameter is the element(s) to replace, the second is the new element(s), and the third is the array being processed.

<code>$arr = array("apple", "banana", "orange");
$new_arr = str_replace($arr, strtoupper($arr), $arr);
print_r($new_arr); // Output: Array ( [0] => APPLE [1] => BANANA [2] => ORANGE )</code>

Multiple replacements:

str_replace() can perform multiple replacements at once. Both the search and replace parameters can be arrays or strings, and the subject can also be an array or string.

<code>$str = "Hello, my name is John.";
$search = array("Hello", "name", "John");
$replace = array("Hi", "nickname", "Mike");
$new_str = str_replace($search, $replace, $str);
echo $new_str; // Output: Hi, my nickname is Mike.</code>

Note that str_replace() is case‑sensitive. To perform a case‑insensitive replacement, use str_ireplace() .

Summary:

str_replace() is a PHP function for replacing specified characters or strings. It is highly flexible, supporting single characters, strings, arrays, multiple replacements, and case‑insensitive operations via str_ireplace() . Mastering this function greatly enhances string manipulation capabilities.

PHP learning recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Mastery Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

backendPHPstring replacementphp tutorialstr_replace
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.