weird question. But I need to explode a string by uppercase letters. So if I had:

$uc = 'UpperCase';

///Would become

print $uc[0] /// 'Upper';
print $uc[1] /// 'Case';

I need it because I'm using mod_rewrite and I can't send URL variables with anything but a continuous word.

    I don't think explode would be right for this - I'm guessing preg_match with some godawful regex would be what you're looking for. Nogdog? 😃

    Edit: Just beat me - and yeah, preg_split would be better than match, I suppose.

      What would be the preg_split syntax

        Edit: Just beat me - and yeah, preg_split would be better than match, I suppose.

        Awe. cheer up mate! I've learned tons from your posts as well as Nog's and gang, so here is a heartfelt thanks ....!

          Well to use it you need to understand regular expressions, but basically something like this should work:

          $array = preg_split("/[A-Z]{1}[a-z0-9]*/", $uc);

            I don't understand regular expressions well. When I test it I get a two part array for words with a single capital letter and three with two capitals. However, the array parts are blank

              this is closer but I'm still missing the capital letter

              $array = preg_split("/[A-Z]/", $uc, -1, PREG_SPLIT_NO_EMPTY);

                Thanks for your help. But I'm getting:

                Array ( [0] => pper [1] => ase)

                Missing the Capital.

                  This returns the capital as an individual element, I guess you could concatenate and build up a new array, this is kind of rudimentary though.

                  $array = preg_split("/([A-Z])/", $uc, -1, PREG_SPLIT_DELIM_CAPTURE);

                  You could make your life easier by including under score keys or spaces in your string, then you could simply match those.

                    $words = preg_split('/(?=[A-Z])/', $string, -1, PREG_SPLIT_NO_EMPTY);
                    

                      Nice trick Nog. I didn't realize the lookahead construct didn't consume the target string, pretty cool, thanks.

                        luiddog wrote:

                        I need it because I'm using mod_rewrite and I can't send URL variables with anything but a continuous word.

                        Would-it-be-easier-to-use-hyphens-to-separate-words?

                          I get errors when I use anything including hyphens

                            Write a Reply...