0

From what I understand youtube.com uses three types of urls for their video links.

http://www.youtube.com/watch?v=8uLPtmCroQ8&feature=related
http://www.youtube.com/watch?v=8uLPtmCroQ8
http://youtu.be/8uLPtmCroQ8

I get this url submitted to my site in any one of these different ways and I store the url into a custom field called $video_code. I need to strip it of any parameters that come after the id of the video so if a user submit the first url above, &feature=related gets stripped. I'm using php.

Pollux Khafra
  • 862
  • 5
  • 17
  • 32
  • The `v` parameter won't always be first. You shouldn't rely on that. Also, this was asked yesterday: http://stackoverflow.com/a/10387324/362536 – Brad May 01 '12 at 20:27
  • Ok what I need to do is pull out the ID from any type of youtube video url. The question you postes isnt going to work for youtu.be urls will it? I was going to use this http://stackoverflow.com/questions/6556559/youtube-api-extract-video-id/6556662#6556662 but it doesn't work if the url has parameters after the id. – Pollux Khafra May 01 '12 at 20:35

5 Answers5

2

If I understand your problem correctly, You could use something like this to store the video id in the databse and then construct the url as you like.

function getVideoId($url)
{
    $parsedUrl = parse_url($url);
    if ($parsedUrl === false)
        return false;

    if (!empty($parsedUrl['query']))
    {
        $query = array();
        parse_str($parsedUrl['query'], $query);
        if (!empty($query['v']))
            return $query['v'];
    }

    if (in_array(strtolower($parsedUrl['host']), array('youtu.be', 'www.youtu.be')))
        return trim($parsedUrl['path'], '/');

    return false;
}

$input = array('http://www.youtube.com/watch?v=8uLPtmCroQ8&feature=related', 'http://www.youtube.com/watch?v=8uLPtmCroQ8', 'http://youtu.be/8uLPtmCroQ8');

foreach ($input as $url)
{
    echo getVideoId($url) . PHP_EOL;
}
mpratt
  • 1,598
  • 12
  • 21
  • I made it a little misleading making it sound like I stored all three url's into the custom field when I actually meant that the url could be either one of them. But this works perfectly for the way I need it. Thanks. – Pollux Khafra May 01 '12 at 20:51
0

In which language did you want to this? If it is in PHP you should look at this.

0

You could also do a regular expressions to split the string. Take a look here: http://www.php.net/manual/en/function.preg-split.php

Robert H
  • 11,520
  • 18
  • 68
  • 110
0

Use this code:

$arr=array(
'http://www.youtube.com/watch?v=8uLPtmCroQ8&feature=related',
'http://www.youtube.com/watch?v=8uLPtmCroQ8',
'http://youtu.be/8uLPtmCroQ8',
);
for($i=0; $i<count($arr); $i++){
   $urlarr = parse_url($arr[$i]);
   if (!empty($urlarr['query'])) {
      parse_str($urlarr['query']);
      $qarr = array();
      if (!empty($v))
         $qarr['v'] = $v;
      $urlarr['query'] = http_build_query($qarr);
      $arr[$i] = http_build_url('', $urlarr);
   }
}
print_r($arr);

OUTPUT:

Array
(
    [0] => http://www.youtube.com/watch?v=8uLPtmCroQ8
    [1] => http://www.youtube.com/watch?v=8uLPtmCroQ8
    [2] => http://youtu.be/8uLPtmCroQ8
)
anubhava
  • 761,203
  • 64
  • 569
  • 643
0
function getVideoCode($url){
$videoCode;
$code_parse = parse_url($url);
if(empty($code_parse["query"])){
    $videoCode = str_replace("/"," ",$code_parse["path"]);
}else{
    $videoCode = clearQuery($code_parse["query"]);
}
echo $videoCode;
}
function clearQuery($query){
$redundant = array("v", "&", "feature","=","related"); 
return str_replace($redundant," ",$query);
}

It is not a professional code but It's easy to understand.When I call like this:

getVideoCode("http://youtu.be/8uLPtmCroQ8");
getVideoCode("http://www.youtube.com/watch?v=8uLPtmCroQ8");
getVideoCode("http://www.youtube.com/watch?v=8uLPtmCroQ8&feature=related");

The Output is

  • 8uLPtmCroQ8
  • 8uLPtmCroQ8
  • 8uLPtmCroQ8