Alaouy\Youtube\Youtube::parseVIdFromURL PHP Method

parseVIdFromURL() public static method

Support both full URL (www.youtube.com) and short URL (youtu.be)
public static parseVIdFromURL ( string $youtube_url ) : string
$youtube_url string
return string Video Id
    public static function parseVIdFromURL($youtube_url)
    {
        if (strpos($youtube_url, 'youtube.com')) {
            if (strpos($youtube_url, 'embed')) {
                $path = static::_parse_url_path($youtube_url);
                $vid = substr($path, 7);
                return $vid;
            } else {
                $params = static::_parse_url_query($youtube_url);
                return $params['v'];
            }
        } else {
            if (strpos($youtube_url, 'youtu.be')) {
                $path = static::_parse_url_path($youtube_url);
                $vid = substr($path, 1);
                return $vid;
            } else {
                throw new \Exception('The supplied URL does not look like a Youtube URL');
            }
        }
    }

Usage Example

 /**
  *
  *
  * @expectedException \Exception
  */
 public function testParseVIdException()
 {
     $vId = $this->youtube->parseVIdFromURL('http://www.facebook.com');
 }