Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP url to array

Tags:

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.

like image 248
eric Avatar asked Nov 09 '11 15:11

eric


People also ask

Can I pass an array in URL?

You can make use of serialize() and urlencode PHP built-in function to pass an array as URL param. The serialize() function will return a sequence of bits for the input given and the urlencode will again encode the values as well the special characters available in it.

How parse URL in PHP?

PHP | parse_url() Function The parse_url() function is an inbuilt function in PHP which is used to return the components of a URL by parsing it. It parses an URL and return an associative array which contains its various components.

What is $_ GET in PHP?

PHP $_GET is a PHP super global variable which is used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. Assume we have an HTML page that contains a hyperlink with parameters: <html> <body>

What is Http_build_query?

The http_build_query() function is an inbuilt function in PHP which is used to generate URL-encoded query string from the associative (or indexed) array.


1 Answers

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.

like image 105
Oldskool Avatar answered Sep 27 '22 23:09

Oldskool