<?
$data = "The cat likes to sit on the fence. He also likes to climb the tree.";
$find ="/the/";
$replace ="a";
//1. replace single word
Echo "$data <br>";
Echo preg_replace ($find, $replace, $data);
//create arrays
$find2 = array ('/the/', '/cat/');
$replace2 = array ('a', 'dog');
//2. replace with array values
Echo preg_replace ($find2, $replace2, $data);
//3. Replace just once
Echo preg_replace ($find2, $replace2, $data, 1);
//4. Keep a count of replacements
$count = 0;
Echo preg_replace ($find2, $replace2, $data, -1, $count);
Echo "<br>You have made $count replacements";
?>
<?
$data = "The cat likes to sit on the fence. He also likes to climb the tree.";
$find ="/the/";
$replace ="a";
//1. replace single word
Echo "$data <br>";
Echo preg_replace ($find, $replace, $data);
//create arrays
$find2 = array ('/the/', '/cat/');
$replace2 = array ('a', 'dog');
//2. replace with array values
Echo preg_replace ($find2, $replace2, $data);
//3. Replace just once
Echo preg_replace ($find2, $replace2, $data, 1);
//4. Keep a count of replacements
$count = 0;
Echo preg_replace ($find2, $replace2, $data, -1, $count);
Echo "<br>You have made $count replacements";
?>
The cat likes to sit on the fence. He also likes to climb the tree. <br>The cat likes to sit on a fence. He also likes to climb a tree.The dog likes to sit on a fence. He also likes to climb a tree.The dog likes to sit on a fence. He also likes to climb the tree.The dog likes to sit on a fence. He also likes to climb a tree.<br>You have made 3 replacements