How to replace a word inside a string in PHP ?
Last Updated :
15 Jul, 2025
Given a string containing some words the task is to replace all the occurrences of a word within the given string str in PHP. To do this task, we have the following methods in PHP:
Method 1: Using str_replace() Method
The str_replace() method is used to replace all the occurrences of the word W1 by replacing word W2 in the given string str.
Syntax:
str_replace( $searchVal, $replaceVal, $subjectVal, $count )
Example:
PHP
<?php
// PHP program to replace all occurrence
// of a word inside a string
// Given string
$str = "geeks for Geeks";
// Word to be replaced
$w1 = "geeks";
// Replaced by
$w2 = "GEEKS";
// Using str_replace() function
// to replace the word
$str = str_replace($w1, $w2, $str);
// Printing the result
echo $str;
?>
Method 2: Using str_ireplace() Method
The str_ireplace() method is used to replace all the occurrences of the word W1 by replacing word W2 in the given string str. The difference between str_replace() and str_ireplace() is that str_ireplace() is a case-insensitive.
Syntax:
str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )
Example:
PHP
<?php
// PHP program to replace
// a word inside a string
// Given string
$str = "geeks for Geeks";
// Word to be replaced
$w1 = "geeks";
// Replaced by
$w2 = "GEEKS";
// Using str_ireplace() function
// replace the word
$str = str_ireplace($w1, $w2, $str);
// Printing the result
echo $str;
?>
Method 3: Using preg_replace() Method
The preg_replace() method is used to perform a regular expression for search and replace the content.
Syntax:
preg_replace( $pattern, $replacement, $subject, $limit, $count )
Example:
PHP
<?php
// PHP program to replace
// a word inside a string
// Given string
$str = "geeks for Geeks";
// Word to be replaced
$w1 = "geeks";
// Replaced by
$w2 = "GEEKS";
// Using preg_replace() function
// to replace the word
$str = preg_replace('/bgeeksb/',$w2, $str);
// Printing the result
echo $str;
?>
Method 4: Using strtr()
Using strtr() in PHP replaces specified words or characters in a string. It accepts an array where keys are search strings and values are replacements, ensuring precise substitution
Example: In this example we replaces the substring "World" with "PHP" in the string "Hello, World!" using strtr() and prints the modified string: "Hello, PHP!".
PHP
<?php
$string = "Hello, World!";
$replace = array("World" => "PHP");
$newString = strtr($string, $replace);
echo $newString;
?>
Method 5: Using preg_replace_callback()
The preg_replace_callback() function in PHP allows for replacing occurrences of a pattern in a string using a callback function. This method is particularly useful when you need to perform more complex replacements or transformations based on each match found.
Example: In this example, we will replace all occurrences of the word "apple" with "orange" in a given string using preg_replace_callback().
PHP
<?php
$string = "I have an apple, he has an apple, she likes apples.";
// Case-insensitive whole word match for "apple"
$pattern = "/\bapple\b/i";
$result = preg_replace_callback(
$pattern,
function ($matches) {
// Replace "apple" with its uppercase version
return strtoupper($matches[0]);
},
$string
);
echo $result;
?>
OutputI have an APPLE, he has an APPLE, she likes apples.
Method 6: Using substr_replace() Method
The substr_replace() method in PHP allows you to replace a part of a string with another string. While it's typically used for more specific replacements by specifying start and length parameters, it can be adapted to replace all occurrences of a word by looping through the string.
Example
PHP
<?php
// Function to replace all occurrences of a word using substr_replace
function replace_all_occurrences($string, $search, $replace) {
$offset = 0;
while (($pos = strpos($string, $search, $offset)) !== false) {
$string = substr_replace($string, $replace, $pos, strlen($search));
$offset = $pos + strlen($replace);
}
return $string;
}
// Original string
$string = "Hello World! World is beautiful.";
// Replace all occurrences of "World" with "PHP"
$modifiedString = replace_all_occurrences($string, "World", "PHP");
// Output the result
echo $modifiedString;
?>
OutputHello PHP! PHP is beautiful.