簡體   English   中英

PHP中某些字符串處理的最有效方法?

[英]Most efficient way of certain string manipulation in PHP?

我有需要像這樣轉換的字符串,

'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

這難道不能更容易或更有效地實現嗎?

您的駝峰代碼已經很好。 對於第二個,您可以放棄拆分和內爆:

$str = 'helloWorld';
$str = strtolower(preg_replace('/(?<=\\w)([A-Z])/', ' \\1', $str));
echo $str;
// output: hello world

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM