[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Finding values in a string

Posted on 2009-04-07
4
Medium Priority
?
307 Views
Last Modified: 2012-05-06
Hi Experts,

I have some data in a string, I want to find a certain character and then display information after that character and before the next comma e.g.

The string:-
MC=84895FE1;MS=0;SD=288362944;GSID=5550;GT=-1;PC=0;RU=3 100 10000 0 1 10 0 1 0 -1 0 -1 -1 1 ;S=HlIlIIIllIllIIIlI,4FE4F347,0,TT,-1,3,-1,-1,0,1,-1,:HE.LeOwNzAll,5464935A,0,TT,0,8,-1,-1,0,1,-1,E.:HMindstate,3EC23EBF,8088,

I want to return the values after this marker ':H' e.g. 'E.LeOwNzAll' and Mindstate.

I do not want to return the string position as these values can change based on the length of a name.

Thanks in advance
0
Comment
Question by:EzEApostle
[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
4 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24092142
Try this:
function FindData($data,$start=':H',$end=',') {
  $res = array();
  $pos = strpos($data,$start);
  while($pos!==false) {
    $data = substr($data,$pos+strlen($start));
    $res[]=substr($data,0,strpos($data,$end));
    $pos = strpos($data,$start);
  }
  return $res;
}

Open in new window

0
 
LVL 1

Author Comment

by:EzEApostle
ID: 24092204
Thanks cxr, how would I go about using this function with the string I have? Kind of beginner.intermediate with PHP :)
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24092353
Your string is stored in $data. Then call the function like this:

$found = FindData($data);

$found now contains an array with the found items. You can loop over them with a standard foreach:
echo count($found).' items found<br />';
echo '<ul>';
foreach($found as $item)
  echo '<li>'.$item.'</li>';
echo '</ul>';

Open in new window

0
 
LVL 4

Expert Comment

by:bleach77
ID: 24093810
Use the code below. Assume $data = fullstring
The result will be in $matches[1][*]

You can use foreach($matches[1] as $match)
preg_match_all('|:H([^,]+)|', $data, $matches);

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

649 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