允许视频嵌入评论/文本中

发布于 2024-10-06 05:39:42 字数 174 浏览 2 评论 0原文

wordpress 如何允许您仅通过 url 嵌入 youtube/dailymotion/vimeo 视频?例如,如果您输入 [youtube=http://www.youtube.com/watch?v=cXXm696UbKY],视频将显示为嵌入其中。 无论如何,有没有办法使用 Markdown 在 php 中安全地执行此操作?

How does wordpress allow you to embed youtube/dailymotion/vimeo videos by just the url? For example if you type in [youtube=http://www.youtube.com/watch?v=cXXm696UbKY], the video will appear embedded there.
Is there anyway to do this safely in php using markdown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2024-10-13 05:39:42

大多数(全部?)视频平台都提供 oEmbed 支持。

例如,对于 YouTube 视频 http://www.youtube.com/watch?v=cXXm696UbKY,它是 http://www.youtube.com/oembed?url=http%3A //www.youtube.com/watch%3Fv%cXXm696UbKY

这将返回一个响应,您可以使用 json_decode 轻松解析该响应。

{
    "provider_url": "http:\/\/www.youtube.com\/",
    "title": "Auto-Tune the News #8: dragons. geese. Michael Vick. (ft. T-Pain)",
    "html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>",
    "author_name": "schmoyoho",
    "height": 344,
    "thumbnail_width": 480,
    "width": 425,
    "version": "1.0",
    "author_url": "http:\/\/www.youtube.com\/user\/schmoyoho",
    "provider_name": "YouTube",
    "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/bDOYN-6gdRE\/hqdefault.jpg",
    "type": "video",
    "thumbnail_height": 360
}

有趣的部分是 html 属性。

因此,我们所要做的就是在文本中搜索 [YouTube=...] 标签,提取 YouTube URL 并通过 oEmbed 检索嵌入代码。

这是一个工作示例:

<?php
function getYouTubeCode($url)
{
    $oembedUrl = 'http://www.youtube.com/oembed?url=' . urlencode($url);

    // The @-operator suppresses errors if the YouTube oEmbed service can't handle our request
    $data = @file_get_contents($oembedUrl);

    // If $data contains invalid JSON code, it will return null
    $data = json_decode($data);

    // So if $data is not an object now, we abort
    if (!is_object($data)) {
        return '';
    }

    // Otherwise we return the YouTube embed code
    return $data->html;
}

$text = '<h1>Hi There</h1><p>You gotta watch this video:</p><p>[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]</p>';

$matches = array();

// We scan the $text variable for all occurrences of "[YouTube=<Any character except right squared bracket>]"
if (preg_match_all('/\[YouTube=([^\]]+)\]/i', $text, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
        // Eg. $match[0] is "[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]"
        // and $match[1] is "http://www.youtube.com/watch?v=cXXm696UbKY"
        $text = str_replace($match[0], getYouTubeCode($match[1]), $text);
    }
}

echo $text;

Most (all?) of this video platforms provide oEmbed support.

For example for the YouTube video http://www.youtube.com/watch?v=cXXm696UbKY it is http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch%3Fv%cXXm696UbKY.

This will return a response which you can easily parse with json_decode.

{
    "provider_url": "http:\/\/www.youtube.com\/",
    "title": "Auto-Tune the News #8: dragons. geese. Michael Vick. (ft. T-Pain)",
    "html": "<object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\"><\/param><param name=\"allowFullScreen\" value=\"true\"><\/param><param name=\"allowscriptaccess\" value=\"always\"><\/param><embed src=\"http:\/\/www.youtube.com\/v\/bDOYN-6gdRE?fs=1\" type=\"application\/x-shockwave-flash\" width=\"425\" height=\"344\" allowscriptaccess=\"always\" allowfullscreen=\"true\"><\/embed><\/object>",
    "author_name": "schmoyoho",
    "height": 344,
    "thumbnail_width": 480,
    "width": 425,
    "version": "1.0",
    "author_url": "http:\/\/www.youtube.com\/user\/schmoyoho",
    "provider_name": "YouTube",
    "thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/bDOYN-6gdRE\/hqdefault.jpg",
    "type": "video",
    "thumbnail_height": 360
}

The interesting part is the html property.

So all we have to do is search the text for the [YouTube=...] tag, extract the YouTube URL and retrieve the embed code via oEmbed.

Here a working example:

<?php
function getYouTubeCode($url)
{
    $oembedUrl = 'http://www.youtube.com/oembed?url=' . urlencode($url);

    // The @-operator suppresses errors if the YouTube oEmbed service can't handle our request
    $data = @file_get_contents($oembedUrl);

    // If $data contains invalid JSON code, it will return null
    $data = json_decode($data);

    // So if $data is not an object now, we abort
    if (!is_object($data)) {
        return '';
    }

    // Otherwise we return the YouTube embed code
    return $data->html;
}

$text = '<h1>Hi There</h1><p>You gotta watch this video:</p><p>[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]</p>';

$matches = array();

// We scan the $text variable for all occurrences of "[YouTube=<Any character except right squared bracket>]"
if (preg_match_all('/\[YouTube=([^\]]+)\]/i', $text, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
        // Eg. $match[0] is "[YouTube=http://www.youtube.com/watch?v=cXXm696UbKY]"
        // and $match[1] is "http://www.youtube.com/watch?v=cXXm696UbKY"
        $text = str_replace($match[0], getYouTubeCode($match[1]), $text);
    }
}

echo $text;
惯饮孤独 2024-10-13 05:39:42

我真的不知道wordpress的问题,但基本逻辑是搜索url并将其转换为Youtube嵌入代码,添加周围的东西!我认为 preg_replace() 是你必须牢记的!

I really don't know the issue with wordpress, but the basic logic is to search for url and turn it to Youtube embed code, add the stuff around it! I think preg_replace() is what you have to keep in mind!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文