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

前端 未结 4 1352
轮回少年
轮回少年 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条回答
  •  Happy的楠姐
    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

提交回复
热议问题