Solved

Advanced Extracting Substrings from String

Posted on 2011-03-21
6
326 Views
Last Modified: 2012-05-11
I have a string like so:
" status : good   firstname : jason lastname : alexander"

I'm trying to extract those 3 values into their own seperate substring, so
$status = "good"
$firstname = "jason"
$lastname= "alexander"

I'm having much difficulty trying to do this, however.

I'm using strtok to seperate into substrings but it doesn't seem to be very effective.. here's my current code:
$data = $_GET['data'];
echo $data;

if(strstr($data, "status") != FALSE)
{
$tok = strtok($data, " ");
while ($tok !== false) {

    	$appstring = str_replace(" status : ", "", $tok);
	$tok = strtok(" : ");

	echo $tok;
}

Open in new window


this code just outputs "goodfirstnamejasonlastnamealexander"

I just need to know how to extract those substrings, any help would be greatly appreciated.
Thanks
0
Comment
Question by:JoeD77
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:dimaj
ID: 35182349
try using explode()
Here's a help link: http://us2.php.net/manual/en/function.explode.php
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35182407
Try this

<?php

$data = " status : good   firstname : jason lastname : alexander";

$pattern = '/(\w+)\s+:\s+(\w+)/is';

preg_match_all( $pattern, $data, $matches );


foreach( $matches[1] as $anIndex => $aCode ) {
     echo "$aCode has the value '{$matches[2][$anIndex]}'<br/>";
}

Open in new window



Which produces this

status has the value 'good'
firstname has the value 'jason'
lastname has the value 'alexander'
0
 

Author Comment

by:JoeD77
ID: 35184651
Thanks. That works swell except for one problem:

I also need to be able to handle URLs, and when one is included in $data, it erases the URL.

So
$data = "website : http://hoho.com/ohmy firstname : fred lastname : smith"

Becomes:
"http fred smith"

So basically I need to know how to properly handle URLs. I tried removing the chars "http://" but if the url has a subdirectory in it (www.website.com/mypage) the '/' will obfuscate the URL.

I realize this is slightly off topic from what I posted, so I'd be more than happy to offer more points for assistance with this.

Thanks again.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:JoeD77
ID: 35184702
Also, periods '.' will cause the same obfuscation within the URL.
0
 
LVL 7

Expert Comment

by:dimaj
ID: 35184712
Joe,

Here's an algorithm that might work for you:
1) replace all instances of " : " to ":"
2) split your resulting string based on " "
3) loop through your resulting array
4) for each element in array, split based on ":"
5) Have a simple case statement that checks whether first element is a "website" or "firstname" or "lastname" to extract information that you need.

Hope this helps.

dimaj
0
 

Author Comment

by:JoeD77
ID: 35184906
Thanks for your suggestion. Luckily it was an easy fix of changing the pattern to
"/(\S+)\s+:\s+(\S+)/is"

to allow for special characters

Thanks !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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

12 Experts available now in Live!

Get 1:1 Help Now