YouTube Vimeo Video ID from Embed Code or From URL with PHP Regular Expression RegEx

前端 未结 4 1286
轮回少年
轮回少年 2021-02-01 10:09

I want to get Video ID for YouTube or Vimeo via its Embed code or from URL, Any solution to do this with PHP ?

相关标签:
4条回答
  • 2021-02-01 10:49

    There is another similar [for youtube only] answered here, might be useful to have a look. PHP Regex to get youtube video ID?

    0 讨论(0)
  • 2021-02-01 10:50

    According to the latest youtube standard, you can use following code to extract video id from embed code.

    $embed = '<iframe src="//www.youtube.com/embed/n6WYu-pSXH8" frameborder="0"></iframe>';
    preg_match('~embed/(.*?)"~', $embed, $output);
    echo $output[1];
    

    Thank you

    0 讨论(0)
  • 2021-02-01 10:53

    There is another question with a lot of good answers, specifically from @user2200660.

    https://stackoverflow.com/a/16841070/3850405

    I prefer to use a regex that both can handle embedded code or any URL currently used.

    http://vimeo.com/423630
    https://vimeo.com/1399176
    http://vimeo.com/423630087
    https://vimeo.com/423630087
    https://player.vimeo.com/video/423630087
    https://player.vimeo.com/video/423630087?title=0&byline=0&portrait=0
    https://vimeo.com/channels/staffpicks/423630087
    https://vimeo.com/showcase/7008490/video/407943692
    

    Regex that can handle all URLs I have come across:

    (https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*
    

    https://regex101.com/r/p2Kldc/1/

    $vimeo = 'http://player.vimeo.com/video/67019023';
    
    if(preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/?(showcase\/)*([0-9))([a-z]*\/)*([0-9]{6,11})[?]?.*/", $vimeo, $output_array)) {
        echo "Vimeo ID: $output_array[6]";
    }
    

    Credits to @zeckdude for the original example code in PHP.

    https://stackoverflow.com/a/29860052/3850405

    JavaScript version:

    https://stackoverflow.com/a/65846269/3850405

    0 讨论(0)
  • 2021-02-01 10:58

    You could use preg_match to get the IDs. I will cover the expressions themselves later in this answer, but here is the basic idea of how to use preg_match:

    preg_match('expression(video_id)', "http://www.your.url.here", $matches);
    $video_id = $matches[1];
    

    Here is a breakdown of the expressions for each type of possible input you asked about. I included a link for each showing some test cases and the results.

    1. For YouTube URLs such as http://www.youtube.com/watch?v=89OpN_277yY, you could use this expression:

      v=(.{11})
      
    2. YouTube embed codes can either look like this (some extraneous stuff clipped):

      <object width="640" height="390">
          <param name="movie" value="http://www.youtube.com/v/89OpN_277yY?fs=...
          ...
      </object>
      

      Or like this:

      <iframe ... src="http://www.youtube.com/embed/89OpN_277yY" ... </iframe>
      

      So an expression to get the ID from either style would be this:

      \/v\/(.{11})|\/embed\/(.{11})
      
    3. Vimeo URLs look like http://vimeo.com/<integer>, as far as I can tell. The lowest I found was simply http://vimeo.com/2, and I don't know if there's an upper limit, but I'll assume for now that it's limited to 10 digits. Hopefully someone can correct me if they are aware of the details. This expression could be used:

      vimeo\.com\/([0-9]{1,10})
      
    4. Vimeo embed code takes this form:

      <iframe src="http://player.vimeo.com/video/<integer>" width="400" ...
      

      So you could use this expression:

      player\.vimeo\.com\/video\/([0-9]{1,10})
      

      Alternately, if the length of the numbers may eventually exceed 10, you could use:

      player\.vimeo\.com\/video/([0-9]*)"
      

      Bear in mind that the " will need to be escaped with a \ if you are enclosing the expression in double quotes.


    In summary, I'm not sure how you wanted to implement this, but you could either combine all expressions with |, or you could match each one separately. Add a comment to this answer if you want me to provide further details on how to combine the expressions.

    0 讨论(0)
提交回复
热议问题