youtube 嵌入视频 pregreplace 与开始时间 [英] youtube embedded video pregreplace with start timing

查看:32
本文介绍了youtube 嵌入视频 pregreplace 与开始时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的论坛上,由于以下答案,我们目前将所有 youtube 链接替换为嵌入对象:

On our forum we currently replace all youtube links with embedded objects, thanks to the following answer:

我该怎么做使用正则表达式查找字符串中的所有 YouTube 视频 ID?

问题是,我们的许多用户希望直接发布指向视频中特定时间的链接.例如:

The trouble is, many of our users wish to post a link directly to a particlar time within a video. E.g.:

http://www.youtube.com/watch?v=jECIv7zlD4c&feature=player_embedded#t=1m15s

注意#t=1m15s"

Note the "#t=1m15s"

根据 youtube 文档,当您将开始时间嵌入到视频中时,需要一个开始"参数,您不能使用1m15s"字符串.'start' 是一个基于秒数的值.

As per the youtube documentation, when you are embedding a start time into a video requires a 'start' parameter, you can't use the '1m15s' string. 'start' is a value based on the number of seconds.

<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/jECIv7zlD4c?fs=1&start=75"</param>
<param name="allowFullScreen" value="true"></param>
<param name="allowScriptAccess" value="always"></param>
<embed src="http://www.youtube.com/v/jECIv7zlD4c?fs=1&start=75"
type="application/x-shockwave-flash" allowscriptaccess="always" width="425" height="344">
</embed>
</object>

他们是否可以用&start=75"替换正则表达式中的#1m15s"?

Is their a way to replace '#1m15s' in regex with '&start=75'?

如果不是,您会如何建议使用 PHP 来完成它,以便它会在论坛帖子中递归构建对象(有时人们会发布多个 YouTube 视频链接)?

If not, how would you suggest it would be done with PHP so it will recursivly build the objects in a forum post (sometimes people post multiple youtube video links)?

推荐答案

preg_replace 用于操作字符串.在您的情况下,您必须在更换之前制作一些东西.

preg_replace is used to manipulate string. In your case you have to make some stuff before replacing.

也许你应该尝试改用 preg_replace_callback,给出一个可以进行计算 (X * 60 + Y) 的回调.

Maybe you should try to use instead preg_replace_callback, giving a callback that will make the calculation (X * 60 + Y).

function sumMinSec($matches)
{
    $minSecMatches = array();
    preg_match("/([0-9]*)m([0-9]*)s/", $matches[0], $minSecMatches);
    return "&start=" . ($minSecMatches[1] * 60 + $minSecMatches[2]);
}

然后调用类似的东西:

$url = "http://www.youtube.com/v/jECIv7zlD4c?fs=1#1m23s";
echo preg_replace_callback("/#([0-9]*m[0-9]*s)/", "sumMinSec", $url);

那么结果是:

http://www.youtube.com/v/jECIv7zlD4c?fs=1&start=83

这篇关于youtube 嵌入视频 pregreplace 与开始时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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