用 HTML 嵌入代码替换文本中的 YouTube URL [英] Replace YouTube URL in text with its HTML embed code

查看:54
本文介绍了用 HTML 嵌入代码替换文本中的 YouTube URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在字符串中找到,此函数将嵌入 youtube 视频.

我的问题是只捕获嵌入视频(iframe,如果有更多则只捕获第一个)并忽略字符串的其余部分的最简单方法是什么.

function youtube($string,$autoplay=0,$width=480,$height=390){preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);返回 preg_replace('#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_;-]+)#i',"<div align=\"center\"><iframe title=\"YouTube 视频播放器\" width=\"$width\" height=\"$height\" src=\"http://www.youtube.com/embed/$match[2]?autoplay=$autoplay\"frameborder=\"0\" allowfullscreen></iframe></div>",$字符串);}

解决方案

好的,我想我明白你想要实现的目标了.用户输入一段文本(一些评论或其他内容),您会在该文本中找到 YouTube 网址并将其替换为实际的视频嵌入代码.

这是我修改它的方式:

function youtube($string,$autoplay=0,$width=480,$height=390){preg_match('#(?:http://)?(?:www\.)?(?:youtube\.com/(?:v/|watch\?v=)|youtu\.be/)([\w-]+)(?:\S+)?#', $string, $match);$embed = <<<Youtube<div align="center"><iframe title="YouTube 视频播放器" width="$width" height="$height" src="http://www.youtube.com/embed/$match[1]?autoplay=$autoplay" frameborder=0"allowfullscreen></iframe>

YOUTUBE;返回 str_replace($match[0], $embed, $string);}

由于您已经使用第一个 preg_match() 定位了 URL,因此无需运行另一个正则表达式函数来替换它.让它匹配整个 URL,然后对整个匹配 ($match[0]) 做一个简单的 str_replace().视频代码在第一个子模式 ($match[1]) 中捕获.我使用 preg_match() 因为您只想匹配找到的第一个 URL.如果您想匹配所有 URL,而不仅仅是第一个,则必须使用 preg_match_all() 并稍微修改代码.

这是我的正则表达式的解释:

(?:http://)?# 可选协议,非捕获(?:万维网\.)?# 可选的www.",非捕获(?:# youtube.com/v/XXX"或youtube.com/watch?v=XXX"youtube\.com/(?:v/|watch\?v=)|youtu\.be/# 或youtu.be"缩短网址)([\w-]+) # 视频代码(?:\S+)?# 可选的非空白字符(其他 URL 参数)

This function embed youtube videos if found in a string.

My question is what would be the easiest way to only capture the embedded video (the iframe and only the first if there are more) and ignore rest of the string.

function youtube($string,$autoplay=0,$width=480,$height=390)
{
preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
  return preg_replace(
    '#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_;-]+)#i',
    "<div align=\"center\"><iframe title=\"YouTube video player\" width=\"$width\" height=\"$height\" src=\"http://www.youtube.com/embed/$match[2]?autoplay=$autoplay\" frameborder=\"0\" allowfullscreen></iframe></div>",
    $string);
}

解决方案

Okay, I think I see what you're trying to accomplish. A user inputs a block of text (some comment or whatever), and you find a YouTube URL in that text and replace it with the actual video embed code.

Here's how I've modified it:

function youtube($string,$autoplay=0,$width=480,$height=390)
{
    preg_match('#(?:http://)?(?:www\.)?(?:youtube\.com/(?:v/|watch\?v=)|youtu\.be/)([\w-]+)(?:\S+)?#', $string, $match);
    $embed = <<<YOUTUBE
        <div align="center">
            <iframe title="YouTube video player" width="$width" height="$height" src="http://www.youtube.com/embed/$match[1]?autoplay=$autoplay" frameborder="0" allowfullscreen></iframe>
        </div>
YOUTUBE;

    return str_replace($match[0], $embed, $string);
}

Since you're already locating the URL with the first preg_match(), there's no need to run another regex function for replacing it. Have it match the entire URL, then do a simple str_replace() of your entire match ($match[0]). The video code is captured in the first subpattern ($match[1]). I'm using preg_match() because you only want to match the first URL found. You'd have to use preg_match_all() and modify the code a bit if you wanted to match all URLs, not just the first.

Here's an explanation of my regular expression:

(?:http://)?    # optional protocol, non-capturing
(?:www\.)?      # optional "www.", non-capturing
(?:
                # either "youtube.com/v/XXX" or "youtube.com/watch?v=XXX"
  youtube\.com/(?:v/|watch\?v=)
  |
  youtu\.be/     # or a "youtu.be" shortener URL
)
([\w-]+)        # the video code
(?:\S+)?        # optional non-whitespace characters (other URL params)

这篇关于用 HTML 嵌入代码替换文本中的 YouTube URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆