Grab the video ID from YouTube URL?

前端 未结 5 1895
臣服心动
臣服心动 2021-01-23 11:38

Say I have a list of IDs like so:

http://www.youtube.com/watch?v=KMU0tzLwhbE
http://youtu.be/KMU0tzLwhbE
http://www.youtube.com/watch?v=KMU0tzLwhbE&featured=         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 12:17

    Try something like:

    $pattern = '/(?:(?:\?|&)v=|\/)([A-Za-z0-9]{11})/';
    $url     = 'http://www.youtube.com/watch?v=KMU0tzLwhbE';
    
    preg_match($pattern, $url, $matches);
    
    echo $matches[1];
    

提交回复
热议问题