Inserting a Space Before a Capital Letter
// October 14th, 2008 // PHP
I needed to insert a space between a camel case string, however, some of the strings contained more than one uppercase letter. I needed to have a regular expression that inserted a space only after an uppercase letter that had a lowercase letter following it. Here is the code:
1 2 3 4 5 | //put space after uppercase $string = preg_replace('/(\w+)([A-Z])/U', '\\1 \\2', $string); //put space before uppercase following lowercase $string = preg_replace('/([a-z])([A-Z])/', '\\1 \\2', $string); |
