0

How can I remove all GET variables from a string apart from ?v=

E.g: the below url would remove &feature=related

www.youtube.com/watch?v=PDZ_NhSSiq8&feature=related

Would this be safe to simple remove everything after the first & or is there a more robust way of doing this in PHP?

$safe_url= substr($url, 0, strpos($url, "&"));

2 Answers 2

3

Why not just do something like this:

$url = "http://www.youtube.com/watch?v=PDZ_NhSSiq8&feature=related";
parse_str(parse_url($url, PHP_URL_QUERY), $youtube);
$safe_url = 'http://www.youtube.com/watch?v=' . $youtube['v'];
echo $safe_url;

http://php.net/manual/en/function.parse-url.php

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

Comments

0

Well since $_GET is an array you could recraft the URL using $_GET[v]

something like

$newURL = $_SERVER['SERVER_NAME'].'/'.$_GET[v];

header("Location:".$newURL."");

not sure if thats 100% correct syntactically but you get the idea

2 Comments

sadly this wouldn't work because the user is entering the URL into a form.
its essentially the same answer as Kyle .. he just spoke more eloquently :D

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.