25

I have these urls from a web log:

http://www.domain.com/page?s=194&client=151678&m=a4a&v=1&g=54
http://www.domain.com/page?s=51&client=617171&m=b4z&v=8&g=97

How can I convert this URL in an array in PHPso i will have an array like this

array(
 'page' => array(
 's' => 194,
 'client' => 151678
 'm' => 'a4a',
 'v' => 1,
 'g' => 54
 )
 etc..
)

then later I can loop to that array to find duplicates or validate the information correctly.

LarsTech
81.9k14 gold badges161 silver badges237 bronze badges
asked Nov 9, 2011 at 15:15

6 Answers 6

35

PHP has a native function for that, the parse_str function. You can use it like:

parse_str($_SERVER['QUERY_STRING'], $outputArray);

This will do just what you need.

answered Nov 9, 2011 at 15:25
Sign up to request clarification or add additional context in comments.

1 Comment

This is the preferred method as it supports any standard string that separates by & and =. Works great for curl responses as well
28

There may be a better way to do this but this is what I came up with.

<?php
 $url = 'http://www.domain.com/page?s=194&client=151678&m=a4a&v=1&g=54';
 $remove_http = str_replace('http://', '', $url);
 $split_url = explode('?', $remove_http);
 $get_page_name = explode('/', $split_url[0]);
 $page_name = $get_page_name[1];
 $split_parameters = explode('&', $split_url[1]);
 for($i = 0; $i < count($split_parameters); $i++) {
 $final_split = explode('=', $split_parameters[$i]);
 $split_complete[$page_name][$final_split[0]] = $final_split[1];
 }
 var_dump($split_complete);
?>

http://codepad.org/xTsAks46

answered Nov 9, 2011 at 15:49

Comments

6

With parse_url() you can parse the URL to an associative array. In the result array you get amongst others the query part. Then you can use parse_str() for parsing the query part to your new array.

rid
64k31 gold badges159 silver badges201 bronze badges
answered Nov 9, 2011 at 15:27

Comments

1

Assuming from your question that path will always be /page, the array containing the parsed URLs cannot have multiple page indexes so you should use numeric indexes.

Try this:

$urls_parsed = array();
foreach ($url_strings as $url_string) {
 $url = parse_url($url_string);
 $urls_parsed[] = parse_str($url['query']);
}

$url_strings is an array containing all the URLs you want to parse in string format. $urls_parsed is the resulting array containing the URLs parsed in the format you requested.

Jonathan Spooner
7,7622 gold badges38 silver badges41 bronze badges
answered Nov 9, 2011 at 15:59

Comments

1

Use parse_url function like

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>

Which will give you the following array output

Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)

And from the query you can easily split it as to (key,value) paira. Hope this helps.:)

answered Feb 1, 2014 at 13:13

Comments

0

You could do:

function convertUrlToArray($url){
 $url = str_replace("http://www.domain.com/page?", "", $url);
 $urlExploded = explode("&", $url);
 $return = array();
 foreach ($urlExploded as $param){
 $explodedPar = explode("=", $param);
 $return[$explodedPar[0]] = $explodedPar[1];
 }
 return $return;
}
$url1 = convertUrlToArray("http://www.domain.com/page?s=51&client=617171&m=b4z&v=8&g=97");
var_dump($url1);

example here http://codepad.org/KIDKPzaU

answered Nov 9, 2011 at 15:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.