Mega Code Archive

 
Categories / Php / Code Snippets
 

Find and replace words in a text string using str_replace

<?php // The text string$text = "The quick brown fox jumped over the lazy dog."; // The word we want to replace$oldWord = "brown"; // The new word we want in place of the old one$newWord = "blue"; // Run through the text and replaces all occurrences of $oldText$text = str_replace($oldWord , $newWord , $text); // Display the new textecho $text; ?>