大写"_"后面的字符, 三种方法及其时间比较

$str = 'str_ab ae_dfe';
$time1 = microtime();
for($i = 0; $i<1000; $i++){
    str_replace(
        array(' ', '##'),
        array('', ' '),
        lcfirst(ucwords(str_replace(
            array(' ', '_'),
            array('##', ' '),
            $str
            )))
        );
}
$time2 = microtime();
$str = 'str_ab ae_dfe';
for($i = 0; $i<1000; $i++){
    $str = explode('_', $str);
    foreach($str as $k=>&$v){
        if($k == 0)
            continue;
        $v = ucfirst($v);
    }
    $str = implode('', $str);
}
$time3 = microtime();
echo $time2 - $time1;
echo '
';
echo $time3 - $time2;
echo '
';
$str = 'str_ab ae_dfe';
$t = microtime();
for($i = 0; $i < 1000; $i++){
    preg_replace_callback('|_(\w)|', function($matches){
        return strtoupper($matches[0]);
    }, $str);
}
echo microtime()-$t;
exit;

$str = explode('_', $str);
foreach($str as $k=>&$v){
    if($k == 0)
        continue;
    $v = ucfirst($v);
}
$str = implode('', $str);
echo $str;exit;


echo preg_replace('/_([a-z])/',strtoupper('\\1'),$str);

exit;



$arr = explode(' ', $str);
foreach($arr as &$v){
    $vv = ucwords(str_replace(array('-', '_'), ' ', $v));
    $v = str_replace(' ','',lcfirst($vv));    
}
$str = implode(' ', $arr);
echo $str;

你可能感兴趣的:(功能)