Solved

Advanced Extracting Substrings from String

Posted on 2011-03-21
6
328 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 …

786 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