it misses the last character
add $string[$i-1] to $elements[$j] before doing $j++ at the end of the loop
sorry, wrote this in one shot
Main Topics
Browse All TopicsI am trying to convert a long string of characters into an array, split by character groups.
For example, from the string "AAABCCCCDDAAAAEEE", I need separate strings "AAA" "B" "CCCC" "DD" "AAAA" "EEE"
Thanks for your help.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
VGR,
Little change was needed to produce the required results:
$oldchar=$string[0];
$curchar=$string[$i+1];
no j++ after loop finishes, as follows:
<?
$string="AAABCCCCDDAAAAEEE
$elements=array();
$i=0; // counter of chars
$j=0; // counter of elements
$oldchar=$string[0];
while ($i<strlen($string)) {
$curchar=$string[$i+1];
if ($curchar<>$oldchar) { // add found string before continuing
$elements[$j].=$oldchar;
$oldchar=$curchar;
$j++;
}
else $elements[$j].=$curchar; // if we broke sequence
$i++;
} // while string not parsed completely
for ($i=0; $i<$j;$i++) echo "element $i = '".$elements[$i]."'<BR>";
?>
Business Accounts
Answer for Membership
by: VGRPosted on 2003-03-20 at 11:14:23ID: 8176133
well, no other "regexp-free" choice than :
";
<?
// initialisations
$string="AAABCCCCDDAAAAEEE
$elements=array(); // array[*] of String in high-level languages
// loop
$i=0; // counter of chars
$j=0; // counter of elements
$oldchar=$string[1];
while ($i<strlen($string)) {
$curchar=$string[$i];
if ($curchar<>$oldchar) { // add found string before continuing
$elements[$j].=$oldchar;
$oldchar=$curchar;
$j++;
} else $elements[$j].=$curchar; // if we broke sequence
$i++;
} // while string not parsed completely
$j++; // end of loop
// display
for ($i=0; $i<$j;$i++) echo "element $i = '".$elements[$i]."'<BR>";
?>
produces this :
element 0 = 'AAAA'
element 1 = 'B'
element 2 = 'CCCC'
element 3 = 'DD'
element 4 = 'AAAA'
element 5 = 'EE'