How to replace a word inside a string in PHP ? (original) (raw)
Last Updated : 18 Jul, 2024
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:
Table of Content
- Using str_replace() Method
- Using str_ireplace() Method
- Using preg_replace() Method
- Using strtr()
- Using preg_replace_callback()
- Using substr_replace() Method
**Method 1: Using str_replace() Method
The **str_replace() methodis used to replace all the occurrences of the word **W1 by replacing word **W2 in the given string **str.
**Syntax:
str_replace( searchVal,searchVal, searchVal,replaceVal, subjectVal,subjectVal, subjectVal,count )
**Example:
PHP `
str=strreplace(str = str_replace(str=strreplace(w1, w2,w2, w2,str); // Printing the result echo $str; ?>`
**Method 2: Using str_ireplace() Method
The **str_ireplace() methodis 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,searchVal, searchVal,replaceVal, subjectVal,subjectVal, subjectVal,count )
**Example:
PHP `
str=strireplace(str = str_ireplace(str=strireplace(w1, w2,w2, w2,str); // Printing the result echo $str; ?>`
**Method 3: Using preg_replace() Method
The **preg_replace() methodis used to perform a regular expression for search and replace the content.
**Syntax:
preg_replace( pattern,pattern, pattern,replacement, subject,subject, subject,limit, $count )
**Example:
PHP `
str=pregreplace(′/bgeeksb/′,str = preg_replace('/bgeeksb/',str=pregreplace(′/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 `
replace=array("World"=>"PHP");<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>e</mi><mi>w</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>t</mi><mi>r</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">newString=strtr(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8778em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">wSt</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">in</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mopen">(</span></span></span></span>string,replace = array("World" => "PHP"); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>e</mi><mi>w</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>t</mi><mi>r</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">newString = strtr(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8778em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">n</span><span class="mord mathnormal">e</span><span class="mord mathnormal">wSt</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mopen">(</span></span></span></span>string, replace=array("World"=>"PHP");<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>e</mi><mi>w</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo>=</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>t</mi><mi>r</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">newString=strtr(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8778em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">wSt</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">in</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mopen">(</span></span></span></span>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 `
`
Output
I 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 `
search,search, search,replace) { $offset = 0; while (($pos = strpos($string, search,search, search,offset)) !== false) { string=substrreplace(string = substr_replace(string=substrreplace(string, replace,replace, replace,pos, strlen($search)); offset=offset = offset=pos + strlen($replace); } return $string; } // Original string $string = "Hello World! World is beautiful."; // Replace all occurrences of "World" with "PHP" modifiedString=replacealloccurrences(modifiedString = replace_all_occurrences(modifiedString=replacealloccurrences(string, "World", "PHP"); // Output the result echo $modifiedString; ?>`
Output
Hello PHP! PHP is beautiful.