Solved

Finding values in a string

Posted on 2009-04-07
4
257 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now