str_replace() / str_ireplace()
| Since: | PHP 4(2000) |
|---|
Replaces all occurrences of a specified string within a string with another string. To replace without case sensitivity, use str_ireplace().
Syntax
str_replace($search, $replace, $subject, &$count); // Replaces without distinguishing uppercase and lowercase str_ireplace($search, $replace, $subject, &$count);
Functions
| Function | Description |
|---|---|
| str_replace($search, $replace, $subject) | Returns a string with all occurrences of $search in $subject replaced with $replace. Case-sensitive. |
| str_ireplace($search, $replace, $subject) | Replaces without case sensitivity. All other behavior is the same as str_replace(). |
Return Value
Returns the string after replacement. If you pass a variable as the fourth argument, it will hold the number of replacements made.
Basic String Replacement
Simple string replacement, case-insensitive replacement, and getting the replacement count.
sample_str_replace.php
<?php
// Simple string replacement
$title = "Kiryu Kazuma's story";
echo str_replace("Kiryu Kazuma", "Majima Goro", $title);
// Replace all occurrences
$text = "Kiryu Kazuma and Kiryu Kazuma face off";
echo str_replace("Kiryu Kazuma", "Saejima Taiga", $text);
// str_ireplace() is case-insensitive
echo str_ireplace("kiryu", "MAJIMA", "Kiryu kiryu KIRYU");
// Replace with an empty string to delete matched parts
$tagged = "This is <b>Kiryu Kazuma</b>'s tale";
echo str_replace(["<b>", "</b>"], "", $tagged);
$count = 0;
$result = str_replace("Kiryu", "Kazuma", "Kiryu stands tall, Kiryu never falls", $count);
echo $result;
echo $count;
Running the code produces the following output:
php sample_str_replace.php Majima Goro's story Saejima Taiga and Saejima Taiga face off MAJIMA MAJIMA MAJIMA This is Kiryu Kazuma's tale Kazuma stands tall, Kazuma never falls 2
Bulk Replacement with Arrays
By passing arrays for both the search and replace arguments, you can perform multiple replacements in one call. This pattern is also useful for template-engine-style string assembly.
sample_str_replace2.php
<?php
// Replace multiple strings at once using arrays
$search = ["Kiryu Kazuma", "Majima Goro", "Saejima Taiga"];
$replace = ["Kiryu", "Majima", "Saejima"];
$text = "Kiryu Kazuma, Majima Goro, and Saejima Taiga gathered";
echo str_replace($search, $replace, $text);
// Template-engine-style string assembly
$template = "Dear {{name}}, your order number is {{order}}. Staff: {{staff}}";
$keys = ["{{name}}", "{{order}}", "{{staff}}"];
$values = ["Akiyama Shun", "A-12345", "Nishiki Akira"];
echo str_replace($keys, $values, $template);
// Replacement count also works with array replacement
$count = 0;
$result = str_replace(["Kiryu", "Majima"], ["KK", "MG"], "Kiryu and Majima and Kiryu", $count);
echo $result;
echo $count;
Running the code produces the following output:
php sample_str_replace2.php Kiryu, Majima, and Saejima gathered Dear Akiyama Shun, your order number is A-12345. Staff: Nishiki Akira KK and MG and KK 3
Practical Patterns (HTML Sanitization, Line Ending Normalization)
Common real-world patterns such as stripping HTML tags and normalizing line endings.
sample_str_replace3.php
<?php
// Strip specific HTML tags (useful when strip_tags is too broad)
$html = "<p>Kiryu Kazuma is <b>undefeated</b>.</p>";
$clean = str_replace(["<p>", "</p>", "<b>", "</b>"], "", $html);
echo $clean;
// Normalize line endings to LF
$text = "Line1\r\nLine2\rLine3\nLine4";
$unified = str_replace(["\r\n", "\r"], "\n", $text);
echo $unified;
// URL slug generation (replace spaces with hyphens)
$title = "Kiryu Kazuma vs Majima Goro";
$slug = strtolower(str_replace(" ", "-", $title));
echo $slug;
Running the code produces the following output:
php sample_str_replace3.php Kiryu Kazuma is undefeated. Line1 Line2 Line3 Line4 kiryu-kazuma-vs-majima-goro
Notes
str_replace() replaces all occurrences of a specified substring within a string. By passing arrays for both the search and replace arguments, you can perform multiple replacements in one call. When using arrays, replacements are applied from left to right, so an earlier replacement may affect a subsequent one — keep this in mind.
str_ireplace() is the case-insensitive version. It is useful when processing strings that mix uppercase and lowercase, such as HTML tags or keywords.
For advanced replacements using regular expressions, use preg_replace(). To search within a string, use strpos(). To split a string, use explode().
Order Dependency in Array Replacement: Earlier Results Can Match Later Searches
When replacing multiple strings with arrays, replacements are applied from left to right. An earlier replacement can produce a string that matches a later search pattern.
Problematic example (does not produce the intended result):
str_replace_ng.php
<?php // Intended: "A" → "AB" → "ABC" in stages $result = str_replace(["A", "AB"], ["AB", "ABC"], "A"); echo $result;
Running the code produces the following output:
php str_replace_ng.php ABB
"A" is replaced with "AB" first, then that "AB" is replaced with "ABC", resulting in "ABB" instead of the expected "ABC". When you need controlled sequential replacement, split the calls:
str_replace_ok.php
<?php
// Apply replacements in separate calls to control order
$str = "A";
$str = str_replace("AB", "ABC", $str);
$str = str_replace("A", "AB", $str);
echo $str;
Running the code produces the following output:
php str_replace_ok.php AB
Mismatched Element Counts Between $search and $replace
When the $search and $replace arrays have different numbers of elements, PHP replaces any unmatched search strings with an empty string. This can cause unintended deletions.
Example with mismatched counts:
str_replace_ng2.php
<?php // $replace has fewer elements (2 vs 3) $search = ["Kiryu Kazuma", "Majima Goro", "Saejima Taiga"]; $replace = ["Kiryu", "Majima"]; $text = "Kiryu Kazuma, Majima Goro, and Saejima Taiga"; echo str_replace($search, $replace, $text);
Running the code produces the following output:
php str_replace_ng2.php Kiryu, Majima, and
"Saejima Taiga" is replaced with an empty string and disappears. Always make sure $search and $replace have the same number of elements:
str_replace_ok2.php
<?php // Match the element counts $search = ["Kiryu Kazuma", "Majima Goro", "Saejima Taiga"]; $replace = ["Kiryu", "Majima", "Saejima"]; $text = "Kiryu Kazuma, Majima Goro, and Saejima Taiga"; echo str_replace($search, $replace, $text);
Running the code produces the following output:
php str_replace_ok2.php Kiryu, Majima, and Saejima
If you find any errors or copyright issues, please contact us.