Solved

Finding values in a string

Posted on 2009-04-07
4
297 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 500 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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