?
Solved

parse string

Posted on 2003-03-20
3
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

762 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