Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment (requires an account with 50 reputation)

5 Answers

up vote 23 down vote accepted

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

share|improve this answer
Thank you very much – eric Nov 9 '11 at 15:54
very comprehensive - thanks. – foxybagga Mar 11 at 12:20
add comment (requires an account with 50 reputation)

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.

share|improve this answer
add comment (requires an account with 50 reputation)

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.

share|improve this answer
add comment (requires an account with 50 reputation)

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

share|improve this answer
that works but I am missing the "page" – eric Nov 9 '11 at 15:32
add comment (requires an account with 50 reputation)

PHP has a native function for that:

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

Will do just what you need.

share|improve this answer
2  
how this will work for web log? – Tech4Wilco Nov 13 '11 at 14:47
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.