?
Solved

Advanced Extracting Substrings from String

Posted on 2011-03-21
6
Medium Priority
?
334 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
[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
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

766 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