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
Solved

Finding values in a string

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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