How to replace a word inside a string in PHP
Topic: PHP / MySQLPrev|Next
Answer: Use the PHP
You can use the PHP
str_replace() function to replace all the occurrences of a word within a string.
In the following example the word "facts" is replaced by the word "truth".
ExampleRun this code »
<?php $my_str = 'If the facts do not fit the theory, change the facts.'; // Display replaced string echo str_replace("facts", "truth", $my_str); ?>
str_replace() function is case-sensitive, however if you want to perform case-insensitive match and replace you can use the
Here are some more FAQ related to this topic:
- How to remove white space from a string in PHP
- How to find number of words in a string in PHP
- How to count how many times a substring occurs in a string in PHP