Converting a regular Youtube 'link' into an embedded video

前端 未结 10 1836
别那么骄傲
别那么骄傲 2021-01-30 02:25

My goal: I am trying to allow users to embed a link to a Youtube video in my site, while giving me control over the player\'s settings.

I would like to do this by only a

10条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 03:01

    I think the simplest way to achieve what you're trying to do is the following:

    You're trying to get from this:

    http://www.youtube.com/watch?v=zb-gmJVW5lw
    

    To this:

    
    

    So you can just simply do this:

    #### Use a regular expression to extract the video code
    @video_id = (/([\w-]{11})/.match(@v_url)).to_s
    #### Put this information in the right format
    @embed_code = ""
    

    And then in your view, just do:

    <%= raw(@embed_code) %>
    

提交回复
热议问题