Solved

Advanced Extracting Substrings from String

Posted on 2011-03-21
6
329 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

838 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