Re: [PHP] Separating words based on capital letter
On 4/24/07, Dotan Cohen <[EMAIL PROTECTED]> wrote: I 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. Thanks in advance for any ideas. Dotan Cohen http://dotancohen.com/howto/root_email.php http://lyricslist.com/lyrics/lyrics/112/19/adams_bryan/waking_up_the_neighbours.html Maybe you could post your current code here? That way, we can improve your code instead of wasting our time to completly rewrite a script. Tijnema -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
Dotan Cohen wrote: I 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. You could do this with one regular expression, but if you really want to avoid them here is a 'pure PHP' way: Cheers, Rich -- Zend Certified Engineer http://www.corephp.co.uk "Never trust a computer you can't throw out of a window" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
# [EMAIL PROTECTED] / 2007-04-25 00:16:40 +0300: > I 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. implode(' ', preg_split('~(?=[[:upper:]])~', 'FooBarBaz', -1, PREG_SPLIT_NO_EMPTY)); -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
Roman Neuhauser wrote: implode(' ', preg_split('~(?=[[:upper:]])~', 'FooBarBaz', -1, PREG_SPLIT_NO_EMPTY)); Or just.. preg_replace('/\B[A-Z]/', ' $0', 'FooBarBaz') Arpad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
Thanks, all for the suggestions. Richard, that code is a masterpeice. I have no problems with regexs, though, only with the regex that I wasn't doing properly. Roman, I'm still picking your code apart and trying to understand everything. I see I'll be learning quite a bit from that. Arpad, I'll try your code soon. As it seems to be the shortest, it would suggest that it is most maintainable as well and I'd like to use it. Thanks, everybody. Dotan Cohen http://what-is-what.com/what_is/xhtml.html http://lyricslist.com/lyrics/lyrics/69/64/beatles/sgt_pepper_s_lonely_hearts_club_band.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
On Tue, April 24, 2007 4:16 pm, Dotan Cohen wrote: > I 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. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
Richard Lynch wrote: Why rule out PCRE, which is probably the best weapon for the job? $string = ltrim(preg_replace('([A-Z])', ' \\1', $string)); Don't mean to be pedantic, but you left out the '/. . ./': $string = $string = ltrim(preg_replace('/([A-D])/', " \\1", $string)); _ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
On 25/04/07, Richard Lynch <[EMAIL PROTECTED]> wrote: On Tue, April 24, 2007 4:16 pm, Dotan Cohen wrote: > I 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. I wasn't tuling it out, I meant that _my_ regex wasn't getting me very far. Sorry for the confusion. Dotan Cohen http://dotancohen.com/howto/command_line.php http://lyricslist.com/lyrics/artist_albums/616/yukmouth.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
On 25/04/07, Dotan Cohen <[EMAIL PROTECTED]> wrote: On 25/04/07, Richard Lynch <[EMAIL PROTECTED]> wrote: > On Tue, April 24, 2007 4:16 pm, Dotan Cohen wrote: > > I 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. $string = preg_replace('/(?<=\w)([[:upper:]])/', ' $1', $string); turns "this is OpenSourceCode" to "this is Open Source Code" -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
On 25/04/07, Robin Vickery <[EMAIL PROTECTED]> wrote: $string = preg_replace('/(?<=\w)([[:upper:]])/', ' $1', $string); turns "this is OpenSourceCode" to "this is Open Source Code" Another great solution, Robin, thanks. I've learned a LOT from this thread. Dotan Cohen http://what-is-what.com/what_is/open_office.html http://hardtofindlyrics.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Separating words based on capital letter
Note: several of the folks used "/" as the delimiter. Actually, it can be almost anything that will not be in the $string. Generally, I use "%" simply because it's easier to spot when I forget the delimiters. Also, note Robin's use of the metachar [[:upper:]]. metacharacters can very useful. My favorites are [:punct:] and [:space:] Dotan Cohen wrote: On 25/04/07, Robin Vickery <[EMAIL PROTECTED]> wrote: $string = preg_replace('/(?<=\w)([[:upper:]])/', ' $1', $string); turns "this is OpenSourceCode" to "this is Open Source Code" Another great solution, Robin, thanks. I've learned a LOT from this thread. Dotan Cohen http://what-is-what.com/what_is/open_office.html http://hardtofindlyrics.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php