Hi Mark,
I do not believe it is possible to split a word like that by capital letters with a regular expression split statement... Let me show you what would happen:
$str = "TheString";
$split = split("[A-Z]",$str); // split by capital letters
foreach($split as $num => $value) {
echo "$num = $value\n";
}
Would get you:
0 = \n
1 = he\n
2 = tring\n
Notice it takes off the capital letters from your string...
However, there are several other alternatives...
1: Turn every capital letter into a <space>capital letter (For example, change "A" into " A") and then split by all " " (spaces) in the string.
2: Write a custom function like I did because splitting by spaces may not always work exactly how you want...
Here is the function I wrote:
function splitByCapitals($str) {
$length = strlen($str);
for($i=0; $i < $length; $i++) {
$cur_char = substr($str,$i,1);
if(ereg("[A-Z]+",$cur_char)) { // letter is a capital
$cur_record = $i;
}
$matches[$cur_record] .= $cur_char; // add current character to current record
}
return $matches;
}
$str = "TheString";
$caps = splitByCapitals($str);
foreach($caps as $num => $value) {
echo "$num = $value\n";
}
Which outputs:
0 = The
3 = String
The $num in the foreach loop is the position in $str where the capital letter for the value is at.
Hope this helps.
-Josh