Jump to content

explode based on uppercase?


scliburn

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.