Solved

Advanced Extracting Substrings from String

Posted on 2011-03-21
6
327 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 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