preg_replace examples

()

Useage

preg_replace ( search_for, replace_with, your_data , optional_limit, optional_count )

Examples


$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 
"; 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 "
You have made $count replacements";

wordpress blog stats