用视频播放器代替YouTube链接
Replace YouTube Link with video Player
我运行一个论坛,并希望自动替换任何链接到YouTube视频与YouTube视频播放器。我在网上找不到这样的东西,但我在Wordpress中见过。
我正在运行PHP。
这就是我要说的:
http://en.support.wordpress.com/videos/youtube/在SO上有很多很多关于重新定义Youtube视频id的问题-只需做谷歌或网站搜索。我冒昧地修改了ridgerrunner的这个答案,以便做你想做的。用嵌入代码替换Youtube URL。如果需要,请查看并编辑模式或嵌入代码。例如,您可能希望将嵌入的视频包装在div
.
<?php
// Replace Youtube URLs with embed code
function embedYoutube($text)
{
$search = '~
# Match non-linked youtube URL in the wild. (Rev:20130823)
(?:https?://)? # Optional scheme.
(?:[0-9A-Z-]+'.)? # Optional subdomain.
(?: # Group host alternatives.
youtu'.be/ # Either youtu.be,
| youtube # or youtube.com or
(?:-nocookie)? # youtube-nocookie.com
'.com # followed by
'S* # Allow anything up to VIDEO_ID,
[^'w's-] # but char before ID is non-ID char.
) # End host alternatives.
(['w-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^'w-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%'w.-]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[''"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%'w.-]* # Consume any URL (query) remainder.
~ix';
$replace = '<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/$1?fs=1"</param>
<param name="allowFullScreen" value="true"></param>
<param name="allowScriptAccess" value="always"></param>
<embed src="http://www.youtube.com/v/$1?fs=1"
type="application/x-shockwave-flash" allowscriptaccess="always" width="425" height="344">
</embed>
</object>';
return preg_replace($search, $replace, $text);
}
$string = 'This is the forum post content with some Youtube links:'."'n".
'http://www.youtube.com/watch?v=NLqAF9hrVbY'."'n".
'http://www.youtube.com/v/u1zgFlCw8Aw?fs=1&hl=en_US';
echo embedYoutube($string);
?>
你不需要手工生成可嵌入的HTML, Youtube支持oEmbed协议:http://oembed.com/#section5
您可以尝试生成玩家代码的小类- http://github.com/chernikovalexey/Livar。我发现它很有趣;)
这是我对Viktor的修改版本
/**
* Finds youtube videos links and makes them an embed.
* search: http://www.youtube.com/watch?v=xg7aeOx2VKw
* search: http://www.youtube.com/embed/vx2u5uUu3DE
* search: http://youtu.be/xg7aeOx2VKw
* replace: <iframe width="560" height="315" src="http://www.youtube.com/embed/xg7aeOx2VKw" frameborder="0" allowfullscreen></iframe>
*
* @param string
* @return string
* @see http://stackoverflow.com/questions/6621809/replace-youtube-link-with-video-player
* @see http://stackoverflow.com/questions/5830387/how-to-find-all-youtube-video-ids-in-a-string-using-a-regex
*/
function generateVideoEmbeds($text) {
// No youtube? Not worth processing the text.
if ((stripos($text, 'youtube.') === false) && (stripos($text, 'youtu.be') === false)) {
return $text;
}
$search = '@ # Match any youtube URL in the wild.
[^"''](?:https?://)? # Optional scheme. Either http or https; We want the http thing NOT to be prefixed by a quote -> not embeded yet.
(?:www'.)? # Optional www subdomain
(?: # Group host alternatives
youtu'.be/ # Either youtu.be,
| youtube'.com # or youtube.com
(?: # Group path alternatives
/embed/ # Either /embed/
| /v/ # or /v/
| /watch'?v= # or /watch'?v=
) # End path alternatives.
) # End host alternatives.
(['w'-]{8,25}) # $1 Allow 8-25 for YouTube id (just in case).
(?: # Group unwanted &feature extension
[&'w-=%]* # Either &feature=related or any other key/value pairs
)
'b # Anchor end to word boundary.
@xsi';
$replace = '<iframe width="560" height="315" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>';
$text = preg_replace($search, $replace, $text);
return $text;
}
你好,我需要相同的代码,但我的内容得到HTML + YouTube url
所以我更新了reg pattern
private function generateVideoEmbeds($text)
{
// No youtube? Not worth processing the text.
if ((stripos($text, 'youtube.') === false) && (stripos($text, 'youtu.be') === false))
{
return $text;
}
$replace = '<iframe width="560" height="315" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>';
$text = preg_replace("/http:'/'/(www.)?(youtube.com|youtube.be)'/watch'?v=['w]{8,25}[^< ]/si", $replace, $text);
return $text;
}
相关文章:
- 我如何使这个YouTube链接在iOS上工作
- 修复Javascript代码以将文本中的所有Youtube链接转换为Youtube ID功能
- 用Javascript正则表达式替换iframe到可点击的YouTube链接
- 替换文本中的随机youtube链接以使用javascript嵌入代码
- 带有 Youtube 链接的 JW 播放器在 IE9 上不起作用
- 如何检测网址/图片/YouTube链接并在其周围连接标签
- rails应用程序不会在youtube链接中添加“;嵌入”;
- 嵌入youtube链接不适用于chrome,但适用于IE
- 从Javascript/Regex中的youtube链接中提取视频ID
- 强制Youtube链接打开在Android/iOS应用程序
- 获取两个值之间的YouTube链接并替换为iframe
- 解析链接,youtube链接从文本
- 随机设置youtube链接以在嵌入视频中播放
- 用iframe替换YouTube链接"使用ASP.NET + c#
- 如何从JSON文件嵌入youtube链接
- 用视频播放器代替YouTube链接
- 强制在应用程序中打开youtube链接,而不是Safari iOS
- 图形API从我的facebook页面获取facebook Youtube链接
- 嵌入视频下载在我的电脑,而不是youtube链接
- 为不同的图像分配不同的链接,并让它在iframe容器上播放指定的(youtube)链接