Home Apache PHP Ubuntu MySQL Linux HTML Win CSS Perl Javascript Rants Retro
 
Print This Page
Date Posted: Sunday 07th of February 2010 | Category: PHP
Using str_replace in PHP. Find and Replace using PHP
You may need to find a certain word within a string and replace this with something else. For example you may be running a small forum and swear words like Windows could be replaced with Linux throughout.

The code below outputs: My favourite operating system is Linux

<?php
$string='My favourite operating system is Windows';
echo str_replace('Windows','Linux', $string);
?>


The code below outputs: Firefox is a cool browser

<?php
$string='Internet Explorer is a rubbish browser';

$string1 = array('Internet Explorer', 'rubbish');
$string2 = array('Firefox', 'cool');

echo str_replace($string1, $string2, $string);
?>