3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<? $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"; ?>
Output for 5.4.34 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
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
Output for 4.3.0 - 5.4.32
<? $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"; ?>