I am trying to parse url and extract value from it .My url value is www.mysite.com/register/?referredby=admin
. I want to get value admin
from this url. For this, I have written following code. Its giving me value referredby=admin
, but I want only admin
as value. How Can I achieve this? Below is my code:
<?php
$url = $current_url="//".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
setcookie('ref_by', parse_url($url, PHP_URL_QUERY));
echo $_COOKIE['ref_by'];
?>
Is not a really clean solution, but you can try something like:
You can use
parse_str()
function.Try this code,
I don't know if it's still relevant for you, but maybe it is for others: I've recently released a composer package for parsing urls (https://www.crwlr.software/packages/url). Using that library you can do it like this:
The parse method parses the url and returns an object, the queryArray method returns the url query as array.