Solved

Finding values in a string

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to count occurrences of each item in an array.

749 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