您的位置:首页 > 其它

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

2017-06-20 17:11 246 查看
$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 '<br>';

echo $time3 - $time2;

echo '<br>';

$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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: