[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

parse string

Posted on 2003-03-20
3
Medium Priority
?
217 Views
Last Modified: 2008-03-06
I 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.
0
Comment
Question by:amiller177
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
VGR earned 300 total points
ID: 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'

0
 
LVL 15

Expert Comment

by:VGR
ID: 8176145
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
0
 

Author Comment

by:amiller177
ID: 8176408
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>";
?>
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question