Given a string containing some words and the task is to replace all the occurrences of a word within the given string str in PHP. In order 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.
str_replace( $searchVal, $replaceVal, $subjectVal, $count )
GEEKS for Geeks
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.
str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )
GEEKS for GEEKS
Method 3: Using preg_replace() Method: The preg_replace() method is used to perform a regular expression for search and replace the content.
preg_replace( $pattern, $replacement, $subject, $limit, $count )
geeks for Geeks
- TypeScript | String replace() Method
- PHP program to find the length of the last word in string
- What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
- Which Characters Should Be Escaped Inside A "pre" tag?
- Index inside map() Function
- How to place a div inside an iframe for IE ?
- How to dynamically load JS inside JS ?
- How to add Google map inside html page without using API key ?
- How to apply border inside a table ?
- CSS | page-break-inside Property
- Add the slug field inside Django Model
- How to vertically align text inside a flexbox using CSS?
- How to add custom google search bar inside your web-page?
- How to count text lines inside of DOM element ?
- What purpose does a <script> tag serve inside of a <noscript> tag?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.