Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract query parameters from a URL string in PHP?

Tags:

Users can input URLs using a HTML form on my website, so they might enter something like this: http://www.example.com?test=123&random=abc, it can be anything. I need to extract the value of a certain query parameter, in this case 'test' (the value 123). Is there a way to do this?

like image 963
soren.qvist Avatar asked Jan 24 '11 16:01

soren.qvist


People also ask

How can I get query string values in PHP?

To get the query string from a URL in php, you can use $_GET super global to get specific key value pairs or $_SERVER super global to get the entire string. A query string is a part of a URL that assigns values to specified parameters.

What is used to extract the query parameters from the URL?

QueryParam annotation in the method parameter arguments. The following example (from the sparklines sample application) demonstrates using @QueryParam to extract query parameters from the Query component of the request URL.

What is Querystring PHP?

A query string is a term for adding/passing data in the URL. It is not language specific.

What does $_ GET do 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. When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent to "test_get.

How to get parameters from a URL string in PHP?

- GeeksforGeeks How to get parameters from a URL string in PHP? The parameters from a URL string can be retrieved in PHP using parse_url () and parse_str () functions. Note: Page URL and the parameters are separated by the ? character. parse_url () Function: The parse_url () function is used to return the components of a URL by parsing it.

How to parse a query string from a URL?

The PHP way to do it is using the function parse_url, which parses a URL and return its components. Including the query string. $url = 'www.mysite.com/category/subcategory?myqueryhash'; echo parse_url ($url, PHP_URL_QUERY); # output "myqueryhash" The function parse_str () automatically reads all query parameters into an array.

How to parse query strings into variables in PHP?

With the help of the parse_str () function, you can parse query strings into variables. The syntax of this function is like so: Now, let’s see examples of how to use these two functions for getting the parameters from the URL string.

What is a query string in PHP?

A query string is a part of a URL that assigns values to specified parameters. When building web pages, query strings help us pass and access information to be used on our web pages. When working in php, the ability to easily access and work with query strings is important.


2 Answers

You can use parse_url and parse_str like this:

$query = parse_url('http://www.example.com?test=123&random=abc', PHP_URL_QUERY); parse_str($query, $params); $test = $params['test']; 

parse_url allows to split an URL in different parts (scheme, host, path, query, etc); here we use it to get only the query (test=123&random=abc). Then we can parse the query with parse_str.

like image 185
Arnaud Le Blanc Avatar answered Nov 16 '22 16:11

Arnaud Le Blanc


I needed to check an url that was relative for our system so I couldn't use parse_str. For anyone who needs it:

$urlParts = null; preg_match_all("~[\?&]([^&]+)=([^&]+)~", $url, $urlParts); 
like image 26
Rob Avatar answered Nov 16 '22 16:11

Rob