下划线上的PHP Ucwords

时间:2012-03-05 18:49:38

标签: php

只是好奇是否有人知道在字符串重新替换上使用ucwords()的快捷方法 带空格的下划线?我有一个preg_replace将执行它,但不会添加 中间需要的空间...

// 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

1 个答案:

答案 0 :(得分:0)

如果您不必使用str_replace,请不要使用正则表达式。

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