0

lets say I have an url

http://www.somepage.com/clicker.php?id_campaign=8&id_email=9324&url=https://www.google.com/search?q=dog&sxsrf=ALeKk019eteEpAwVf2Fk4qYo7TiwhuMQ_Q:1596666153666&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiqzqf3jIXrAhUisaQKHRayBWAQ_AUoAXoECBkQAw&biw=1920&bih=921#imgrc=M4wsJO0A7OQfTM

and im trying to get out the parameters id_campaign, id_email and url however, using the code:

$url = htmlspecialchars_decode($url);
$parts = parse_url($url);
parse_str($parts['query'], $query);

when printing $parts['query'] it only gives me:

https://www.google.com/search?q=dog

because rest of the url it consider as another parameter...

so how do I get all the url parameter out of $url?

3
  • 1
    You need really should url_encode google.com/… before putting it in the url. Commented Aug 5, 2020 at 23:05
  • $url is variable i cant modify in any way for other important reasons, i can only change the way i parse parameters out of $url Commented Aug 5, 2020 at 23:09
  • no its not working fine, just try to echo $query['url'] Commented Aug 5, 2020 at 23:11

1 Answer 1

1

In an ideal scenario, the URLs passed within the query would be URL-encoded, as opposed to HTML-encoded.

What you could do in your case is manually replace all & by something temporary and then replace them all back after parsing the URL. This is not exactly pretty, but works:

$url = str_replace('&', '__TEMP__', $url);

$parts = parse_url($url);
parse_str($parts['query'], $query);

$urlParam = str_replace('__TEMP__', '&', $query['url']);

echo $urlParam;

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.