Re: Separating words based on capital letter
| From: | Robin Vickery | Date: | Wed, 25 Apr 2007 09:23:38 +0000 |
| Subject: | Re: Separating words based on capital letter | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-253669@lists.php.net to get a copy of this message | ||
On 25/04/07, Dotan Cohen <dotancohen@gmail.com> wrote:
On 25/04/07, Richard Lynch <ceo@l-i-e.com> wrote: On Tue, April 24, 2007 4:16 pm, Dotan Cohen wrote:$string = preg_replace('/(?<=\w)([[:upper:]])/', ' $1', $string); turns "this is OpenSourceCode" to "this is Open Source Code" -robinI have some categories named in the database as such: OpenSource HomeNetwork I'd like to add a space before each capital letter, ideally not including the first but I can always trim later if need be. As I'm array_walking the database, I have each value at the moment I need it in a string $item. Other than pregging for A-Z how can I accomplish this? I haven't been able to get it down to less than 54 lines of code, which in my opinion means that I'm doing something wrong.Why rule out PCRE, which is probably the best weapon for the job? $string = ltrim(preg_replace('([A-Z])', ' \\1', $string)); You can wrap that in a function or even use create_function for something that simple, in your array_walk.