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? Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/ 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. Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546512 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); Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546513 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!! Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546520 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 ? Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546521 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! Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546523 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. Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546524 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> Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546527 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. Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546529 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. Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546531 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? Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546535 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 Link to comment https://forums.phpfreaks.com/topic/106627-solved-explode-string/#findComment-546539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.