PHP and the parameters of a url valued in a variable

How to take the parameter of a url that is inside a variable of type string

The purpose is to extrapolate the value of a parameter that makes up a url saved in a string type variable. So let’s imagine we have such a valued variable

$string = 'https://domain.ext/test/page?attribute=qwerty123&option=2';

If we wanted to extract the attribute parameter value from this url we should use the following code

$attr = parse_url($string);
parse_str($attr['query'], $query);
echo $query['attribute'];

which will print on the screen the value of the attribute parameter, that is querty123. This also applies to the option parameter.

Pay attention that these are not the POST and GET methods, this procedure is used to extrapolate the parameter from a link already valued in your code, and which does not necessarily come from the page request.

We periodically check the functioning of the links in our articles. If you notice any links that do not work, please let us know in the comments. If you enjoyed the article consider supporting the blog with a small donation. Thank you. Patreon / Ko-fi / Liberapay / Paypal

Leave a Reply

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