2012-03-05 35 views
0

只是好奇,如果有人知道一個快速的方式來使用ucwords()在字符串中刪除 下劃線與空格?我有一個的preg_replace,將做到這一點,但不會在兩者之間添加 需要的空間......PHP下劃線的Ucwords

// this_string_contents -> ThisStringContents 
preg_replace('/(?:^|_)(.?)/e',"strtoupper('$1')",$string); 

和反向

// ThisStringContents -> this_string_contents 
strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2", $string)); 

這將是很好,如果這些人太對稱,上述 會做這樣的事情

* this_is_a_string -> ThisIsAString -> this_is_astring 
* GetURLForString -> get_urlfor_string -> GetUrlforString 
+4

ucwords(str_replace(「_」,「」,$ string)) – 2012-03-05 18:52:38

+0

CakePHP有一個很好的Inflector類。 [Documentation](http://book.cakephp.org/1.3/en/view/1479/Class-methods) - [API(with source)](http://api11.cakephp.org/class_inflector.html) – 2012-03-05 18:52:53

+0

那麼'WoW - > Wo_w'呢? – 2012-03-05 18:53:00

回答

0

,如果你不具備易用的str_replace而是不要使用正則表達式。

str_replace('_', ' ', $string);