- preg_replace_callback: documentation ( source)
- microtime: documentation ( source)
- strtoupper: documentation ( source)
- preg_replace: documentation ( source)
- strtolower: documentation ( source)
<?php
$count = 100;
$start = microtime(true);
for ($i = 0; $i < $count; $i++) {
$str = "abc";
$str = preg_replace_callback('/a/', function($a) { return strtoupper($a[0]); }, $str);
$str = preg_replace_callback('/b/', function($a) { return strtoupper($a[0]); }, $str);
$str = preg_replace_callback('/c/', function($a) { return strtoupper($a[0]); }, $str);
$str = preg_replace_callback('/a/', function($a) { return strtolower($a[0]); }, $str);
$str = preg_replace_callback('/b/', function($a) { return strtolower($a[0]); }, $str);
$str = preg_replace_callback('/c/', function($a) { return strtolower($a[0]); }, $str);
}
echo "Completed in " . (microtime(true) - $start) . " Seconds\n";
$start = microtime(true);
for ($i = 0; $i < $count; $i++) {
$str = "abc";
$str = preg_replace(array(
'/a/e',
'/b/e',
'/c/e',
'/a/e',
'/b/e',
'/c/e',
),
array(
'strtoupper(\'$1\')',
'strtoupper(\'$1\')',
'strtoupper(\'$1\')',
'strtolower(\'$1\')',
'strtolower(\'$1\')',
'strtolower(\'$1\')',
),
$str
);
}
echo "Completed in " . (microtime(true) - $start) . " Seconds\n";
This script was stopped while abusing our resources