douruye5092 2016-03-16 15:12 采纳率: 100%
浏览 38
已采纳

自动检测和自动嵌入YouTube视频。 PHP还是Javascript?

I run a 4chan style imageboard. When users post youtube or soundcloud links, I would like for the board to give a second link which, when clicked, would embed the video.

If I am not mistaken, 4chan uses JS to accomplish this. I have also seen many forum admins using php to do this. I know that wordpress uses php to do this.

If I went with php, I was thinking of going with something based on this: https://www.warpconduit.net/2013/04/13/automatically-embedding-video-using-only-the-url-with-the-help-of-oembed/

For JS I am not entirely sure how to go about it. I would probably use a regex to detect youtube urls and then convert that information to an embed

  • 写回答

1条回答 默认 最新

  • duanou8504 2016-03-17 09:32
    关注

    You don't need RegEx, PHP or oEmbed to do this. Both sites allow iFrame embeds. So when a user gives a link just add the iframe embedding part yourself (around their link) and output that as part of the user's post.

    Use String functions like indexOf to see if the link contains the word "soundcloud" or "youtube" then adjust user's given link to correct format for the iframe embedding.

    Some examples:

    Youtube : format = youtube.com/embed/ + User Link

    User puts https://www.youtube.com/watch?v=KKE0Py64jmk so your code makes it as...

    <iframe width="500" height="300"
    src="https://www.youtube.com/embed/KKE0Py64jmk">
    </iframe>
    

    SoundCloud : format = w.soundcloud.com/player/?url= + User Link

    User puts https://soundcloud.com/thenextmen/sofa-feat-pupajim then your code changes it to...

    <iframe width="500" height="300"
    src="https://w.soundcloud.com/player/?url=https://soundcloud.com/thenextmen/sofa-feat-pupajim">
    </iframe>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?