scliburn Posted September 29, 2008 Share Posted September 29, 2008 Does php have any built in functions to perform an explode or string replace based on upper case characters: Example: AfghanistanTransnationalIssues I want to parse into Afghanistan Transnational Issues, without having to write some regex script. Anyone know if this exists, and if yes, what is the function name? I don't think it does, as I've been over the php site pretty good over the past 8 years or so. Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/ Share on other sites More sharing options...
Maq Posted September 29, 2008 Share Posted September 29, 2008 This is going to be pretty inefficient depending on how much data you have to analyze but... Grab each letter and do if ($letter == strtoupper($letter)) $letter = " " . $letter; else //go to next Sorry for the quick pseudo but I hope you get my idea. Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/#findComment-653341 Share on other sites More sharing options...
aebstract Posted September 29, 2008 Share Posted September 29, 2008 If you do that make sure it isn't just looking for capital letters but capital letters with lower case after and before it, to make sure it is like your example. Also make sure it isn't suppose to be together for whatever reason (can't think of any atm, but im sure there are cases where you put a capital in the word like "McDonalds" or so) Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/#findComment-653346 Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 You'll need a regex... <?php $string = "AfghanistanTransnationalIssues"; $array = preg_split('/([A-Z])/', $string, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($array as $key => $value) { if ($key % 2 == 0) { $temp = $value; } else { $new_arr[$key / 2] = $temp . $value; } } print_r($new_arr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/#findComment-653350 Share on other sites More sharing options...
scliburn Posted September 29, 2008 Author Share Posted September 29, 2008 thanks peeps and thanks for the code example DW. Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/#findComment-653482 Share on other sites More sharing options...
discomatt Posted September 30, 2008 Share Posted September 30, 2008 I like this better <pre><?php $regex1 = '%([a-z])([A-Z])([a-z])%'; $str1 = 'AfghanistanTransnationalIssues'; $rep1 = preg_replace( $regex1, '$1 $2$3', $str1 ); echo $rep1 ?></pre> Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/#findComment-653541 Share on other sites More sharing options...
Maq Posted September 30, 2008 Share Posted September 30, 2008 And it's more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/126342-explode-based-on-uppercase/#findComment-653774 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.