【发布时间】:2012-01-25 23:56:41
【问题描述】:
我有需要像这样转换的字符串,
'hello world' = 'helloWorld'
反过来也一样,
'helloWorld' = 'hello world'
到目前为止,我对这两种转换的所有信息都是第一次,
$str = 'hello world';
$str = lcfirst(str_replace(' ', '', ucwords($str))); // helloWorld
第二个,
$str = 'helloWorld';
$str = preg_split('/(?=[A-Z])/', $str);
$str = strtolower(implode(' ', $str)); // hello world
这不能更容易或更有效地实现吗?
【问题讨论】:
-
这有什么低效的?我觉得很好
-
最有效的方法是最易读的,没有失败。不要为了每个请求 0.04 毫秒而使您的代码令人眼花缭乱。