1

i'm trying to figure out the best way to spilt the following url string:

area-FFFFFF_miles-100_profession-web+developer

basically into the following:

$area = "FFFFFF";
$miles = 100;
$profession = "web developer";

But i've got a brain block and can't really figure out the most efficient way to do it. Any suggestions?

Thanks.

-Edit- I could do with accounting for any missing values, such as:

area-FFFFFF_miles-_profession-web+developer

missing the miles etc.

3 Answers 3

4
$params = explode('_', $input);
foreach($params as $param) {
    $kv = explode('-', $param);
    ${$kv[0]} = str_replace('+', ' ', $kv[1]);
}

This also account for missing values.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems to work spot on. Just what I needed. Thanks. There are a few answers that are virtually the same so thanks to everyone :)
2

You can use preg_split:

preg_split("/[_-]/",...);

5 Comments

That splits but it doesn't split in variables, still need to expand
Brilliant. Didn't think of that. Any way I could account for something like this: area-FFFFFF_miles-_profession-web+developer (For example, missing the miles value?
I don't understand what you mean. miles isn't missing with your string
The value of miles is missing. Try looking at the 2 edits in the original post.
@Ben: ahhh ok now I got you, i didn't understood at a first glance
0

Something like this:

$paramsStr = 'area-FFFFFF_miles-100_profession-web+developer';

$parts = explode('_', $paramsStr);
foreach($parts as $part) {
    $pair = explode('-', $part);
    ${$pair[0]} = $pair[1];
}

1 Comment

Damn, is this some kind of race? >_< And @SiGanteng, you still have ${$kv[1]}, where you need just $kv[1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.