2012-03-05 12 views
0

文字列を置換する際にucwords()を使用する方法が分かっている人はいらっしゃいますか はスペースでアンダースコアを使用していますか?私は、...PHP Ucwords on underders

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

そして、これらがあまりにも対称であればそれはいいだろう

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

逆にそれを行いますにpreg_replaceを持っていますが、間に 必要なスペースを追加しません上記 は、あなたの代わりにstr_replaceを使用ツー持っていない場合は、正規表現を使用しないでください。この

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

ucwords(str_replace( "_"、 ""、$文字列)) –

+0

CakePHPの偉大な語形のクラスを持っています。 [Documentation](http://book.cakephp.org/1.3/ja/view/1479/Class-methods) - [API(ソースあり)](http://api11.cakephp.org/class_inflector.html) –

+0

'WoW - > Wo_w'はどうですか? –

答えて

0

ようなことを行います。

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