Just curious if anyone knows a quick way to use ucwords() on a string replacining
underscores with spaces? I have a preg_replace that will do it, but won’t add the
needed spaces in between…
// this_string_contents -> ThisStringContents
preg_replace('/(?:^|_)(.?)/e',"strtoupper('$1')",$string);
And the reverse
// ThisStringContents -> this_string_contents
strtolower(preg_replace('/([^A-Z])([A-Z])/', "$1_$2", $string));
It would be nice if these were symmetrical too, the above
will do something like this
* this_is_a_string -> ThisIsAString -> this_is_astring
* GetURLForString -> get_urlfor_string -> GetUrlforString
Don’t use regex if you don’t have to- use
str_replaceinstead.