themistral Posted May 21, 2008 Share Posted May 21, 2008 Hi guys, I have a string in this format: This is a String i Want to Explode I want the string to be exploded at all the capital letters - but I want to retain the capital letters so I get 4 strings: This is a String i Want to Explode I'm missing something really simple here... Currently I have split("[A-Z]",$string); but this returns his is a tring i ant to xplode Can anyone out there help me? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 21, 2008 Share Posted May 21, 2008 That's just how split works. You don't get the values that you're splitting on. Just what's around/between them. I don't know if there is a function to do what you want. I don't know enough about regex to be able to write something. But you could possibly just go through looking for capital letters and then add from the beginning to that spot to an array. Quote Link to comment Share on other sites More sharing options...
littledragon Posted May 21, 2008 Share Posted May 21, 2008 Is there a line break between each set as you're shown? In which case : explode("\n", $string); Quote Link to comment Share on other sites More sharing options...
themistral Posted May 21, 2008 Author Share Posted May 21, 2008 No - it's just a field in a database with a long string - not something I set up!! Quote Link to comment Share on other sites More sharing options...
littledragon Posted May 21, 2008 Share Posted May 21, 2008 so what does the string look like? ThisIsAStringIWantToExplode or This Is A String I Want To Explode ? Quote Link to comment Share on other sites More sharing options...
littledragon Posted May 21, 2008 Share Posted May 21, 2008 oh, I see... sorry didn't look properly check out preg_match_all on php.net... this is definitely a job for regex. you'll be glad you learnt it! Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 21, 2008 Share Posted May 21, 2008 <?php $str = "This is a String i Want to Explode"; $pattern = "/(.)([A-Z])/"; $replacement = "\\1\n\\2"; $new = explode("\n", preg_replace($pattern, $replacement, $str)); print_r($new); ?> Gives you: Array ( [0] => This is a [1] => String i [2] => Want to [3] => Explode ) I typed in "php split string on uppercase letter" and found this: http://www.webmasterworld.com/php/3608725.htm Then I changed it to work after testing it locally. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 21, 2008 Share Posted May 21, 2008 <pre> <?php $str = 'This is a String i Want to Explode'; $pieces = preg_split('/(?=[A-Z])/', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($pieces); ?> </pre> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 21, 2008 Share Posted May 21, 2008 <?php $str = 'This is a String i Want to Explode'; $pieces = preg_split('/(?=[A-Z])/', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($pieces); ?> So what's the difference? Just wondering. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 21, 2008 Share Posted May 21, 2008 It uses a lookaround (a positive lookahead), which matches a position in the string rather than an actual character. It eliminates the need to capture or replace. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 21, 2008 Share Posted May 21, 2008 It uses a lookaround (a positive lookahead), which matches a position in the string rather than an actual character. It eliminates the need to capture or replace. Interesting. Where did you learn about how to do that? Quote Link to comment Share on other sites More sharing options...
effigy Posted May 21, 2008 Share Posted May 21, 2008 Regular-Expressions.info is a good resource: http://www.regular-expressions.info/lookaround.html Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.