Say I wanted to convert some_text_string to SomeTextString Would this be the most efficient way to do it? $string = 'some_text_string'; $string = str_replace('_', ' ', $string); $string = ucwords($string); PHP: Likewise, what would be an efficient method to go the opposite way?
I asked a similar question about a month ago. This would be extremely useful when wanting to use WikiWordLinks inside the body of the page, only separated. Please someone pay attention to this chap, and give us both an answer.
$string = 'some_text_string'; $string = str_replace('_', ' ', $string); $string = ucwords($string); $string = str_replace(' ', '', $string); Code (markup): or stack it: $string = 'some_text_string'; $string = ucwords(str_replace('_', ' ', $string)); $string = str_replace(' ', '', $string); Code (markup):
EDIT: A bit too late... Give this a try. (untested) // Format $string = preg_replace('/_([a-z])/e', "strtoupper('$1')", $string); // Reverse $string = preg_replace('/[A-Z]/e', "'_' . strtolower('$0')", $string); PHP:
OK, not the point of this thread but Nico solved for WikiWords also. Way to be, mad rep to you. $string = "ErectADirectoryIsAGeek" ; // Reverse $string = preg_replace('/[A-Z]/e', "' ' . '$0'", $string); echo $string . "<br /><br />" ; // prints Erect A Directory Is A Geek PHP: