2016-03-16 15:12 阅读 33

自动检测和自动嵌入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:

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 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 = + User Link

    User puts so your code makes it as...

    <iframe width="500" height="300"

    SoundCloud : format = + User Link

    User puts then your code changes it to...

    <iframe width="500" height="300"
    点赞 评论 复制链接分享