Get YouTube video ID from URL w/ PHP

后端 未结 3 1784
萌比男神i
萌比男神i 2020-12-18 17:21

I\'m trying to create a function that calls a value from a Wordpress custom field (\"_videourl\" for a YouTube video URL) and then uses PHP trim to cut it down to just the Y

相关标签:
3条回答
  • 2020-12-18 18:05

    Assuming the regex is correct, you could use preg_replace

    $youtubeId = preg_replace('/^[^v]+v.(.{11}).*/', '$1', $url);
    

    You may also be interested in str_replace or substr as alternatives.

    0 讨论(0)
  • 2020-12-18 18:12

    Best solution I have found;

     function GetYouTubeId($url)
     {
     preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
     $youtube_id = $match[1];
     return $youtube_id;
     }
    
    0 讨论(0)
  • 2020-12-18 18:14

    I had to deal with this for a PHP class i wrote a few weeks ago and ended up with a regex that matches any kind of strings: With or without URL scheme, with or without subdomain, youtube.com URL strings, youtu.be URL strings and dealing with all kind of parameter sorting. You can check it out at GitHub or simply copy and paste the code block below:

    /**
     *  Check if input string is a valid YouTube URL
     *  and try to extract the YouTube Video ID from it.
     *  @author  Stephan Schmitz <eyecatchup@gmail.com>
     *  @param   $url   string   The string that shall be checked.
     *  @return  mixed           Returns YouTube Video ID, or (boolean) false.
     */        
    function parse_yturl($url) 
    {
        $pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
        preg_match($pattern, $url, $matches);
        return (isset($matches[1])) ? $matches[1] : false;
    }
    

    To explain the regex, here's a spilt up version:

    /**
     *  Check if input string is a valid YouTube URL
     *  and try to extract the YouTube Video ID from it.
     *  @author  Stephan Schmitz <eyecatchup@gmail.com>
     *  @param   $url   string   The string that shall be checked.
     *  @return  mixed           Returns YouTube Video ID, or (boolean) false.
     */        
    function parse_yturl($url) 
    {
        $pattern = '#^(?:https?://)?';    # Optional URL scheme. Either http or https.
        $pattern .= '(?:www\.)?';         #  Optional www subdomain.
        $pattern .= '(?:';                #  Group host alternatives:
        $pattern .=   'youtu\.be/';       #    Either youtu.be,
        $pattern .=   '|youtube\.com';    #    or youtube.com
        $pattern .=   '(?:';              #    Group path alternatives:
        $pattern .=     '/embed/';        #      Either /embed/,
        $pattern .=     '|/v/';           #      or /v/,
        $pattern .=     '|/watch\?v=';    #      or /watch?v=,    
        $pattern .=     '|/watch\?.+&v='; #      or /watch?other_param&v=
        $pattern .=   ')';                #    End path alternatives.
        $pattern .= ')';                  #  End host alternatives.
        $pattern .= '([\w-]{11})';        # 11 characters (Length of Youtube video ids).
        $pattern .= '(?:.+)?$#x';         # Optional other ending URL parameters.
        preg_match($pattern, $url, $matches);
        return (isset($matches[1])) ? $matches[1] : false;
    }
    
    0 讨论(0)
提交回复
热议问题