Reading $_GET values from a URL String in PHP

Reading $_GET values from a URL String in PHP was something I had been interested in for a while. It was quite simple to read the $_GET values from the current page’s URL (obviously), but what if you were using AJAX and passing a URL to figure out what data needed to be passed/used? There’s several other reasons why you may need this, but I digress.

While looking for a way to read $_GET variables from a string in PHP, I stumbled across this forum post on Stack Exchange.

parse_str( parse_url( $url, PHP_URL_QUERY ), $array );

Usage

$url = 'http://example.com?foo=bar&search=term';
parse_str( parse_url( $url, PHP_URL_QUERY ), $get_args);
echo $get_args['foo']; // bar
echo $get_args['search']; // term

Source: http://stackoverflow.com/questions/3136425/get-the-get-variables-from-a-url-string

Leave a Reply

Your email address will not be published. Required fields are marked *