Just a quick time comparison of PHP’s preg_replace vs. str_replace

Here’s a short and succinct comparison of running time to replace multiple characters in a given string. I ran the test 20 times each, so the numbers you will find are average running times:

$start = microtime();
$str = "23ilrj23oirj23iorj o23irj23klfj23lkjr4ocimior 4r ioj234roij234r io34jrio4jrio34r jio4jr o34jr oi4jr io34 r";
$new_string = preg_replace('/[\w2]/',',',$str);
$end = microtime();
echo($end - $start);
echo "\n";
$start = microtime();
$str = "23ilrj23oirj23iorj o23irj23klfj23lkjr4ocimior 4r ioj234roij234r io34jrio4jrio34r jio4jr o34jr oi4jr io34 r";
$new_string = str_replace(array('2',' ',"\t"),',',$str);
$end = microtime();
echo($end - $start);

The results:

regex: 0.000608
tr: 0.00024099999999999