从YouTube网址参数中提取分钟和秒(ex'02m12s')

时间:2012-07-05 19:14:00

标签: php regex string youtube

从youtube网址中提取分钟和秒数的最佳方法是什么?

http://www.youtube.com/watch?v=TjTbNWhsG28#t=1m40s&foo=1&bar=2

其中1m40s是我需要的字符串,可以使用'#t ='或'& t ='。

也可能在最后(或者在中间,但不太可能。我会将其排除在外)

http://www.youtube.com/watch?v=TjTbNWhsG28?foo=1&bar=2#t=1m40s

'xmxs'中的'p'可以是一位或两位数字,只能传递其中一个

“#t = 10m3s”或  “& T公司= 09m12s”

并且它们都是可选的,如

“#t = 10m”或  “& t = 3s”或  “#吨= 10m03” S

YouTube现在接受'#t ='以及'& t ='这个参数,所以理想情况下我需要同时考虑这两个问题。用户将这些粘贴到博客的评论部分,输出生成嵌入。我目前使用的脚本不会解析这些时间值。

一旦我知道了分钟和秒钟,我将把总秒数传递给使用'start'参数生成嵌入代码的脚本。 https://developers.google.com/youtube/player_parameters#start

我需要一个PHP解决方案。我假设substr()和正则表达式的完美结合。

3 个答案:

答案 0 :(得分:3)

使用此正则表达式((#)|(\&))t=(\d+m)?(\d+s)?

答案 1 :(得分:3)

我发现接受的答案的代码非常混乱。它也不包括小时数。此外,我认为YouTube删除了#个网址。我在我的Advanced Responsive Video Embedder WordPress插件中编写了这个函数代码。我认为与接受的答案相比,它编码非常干净,并且更容易理解。

我希望这会对某人有所帮助。

/**
 * Calculates seconds based on youtube times
 *
 * @author    Nicolas Jonas
 * @license   GPLv3
 * @link      http://nextgenthemes.com
 *
 * @param     string $yttime   The '1h25m13s' part of youtube URLs
 *
 * @return    int   Starttime in seconds
 */
function youtube_time_to_seconds( $yttime ) {

    $format = false;
    $hours  = $minutes = $seconds = 0;

    $pattern['hms'] = '/([0-9]+)h([0-9]+)m([0-9]+)s/'; // hours, minutes, seconds
    $pattern['ms']  =          '/([0-9]+)m([0-9]+)s/'; // minutes, seconds
    $pattern['h']   = '/([0-9]+)h/';
    $pattern['m']   = '/([0-9]+)m/';
    $pattern['s']   = '/([0-9]+)s/';

    foreach ( $pattern as $k => $v ) {

        preg_match( $v, $yttime, $result );

        if ( ! empty( $result ) ) {
            $format = $k;
            break;
        }
    }

    switch ( $format ) {
        case 'hms':
            $hours   = $result[1];
            $minutes = $result[2];
            $seconds = $result[3];
            break;
        case 'ms':
            $minutes = $result[1];
            $seconds = $result[2];
            break;
        case 'h':
            $hours = $result[1];
            break;
        case 'm':
            $minutes = $result[1];
            break;
        case 's':
            $seconds = $result[1];
            break;
        default:
            return false;
    }

    return ( $hours * 60 * 60 ) + ( $minutes * 60 ) + $seconds;
}

来源:Github

可以像这样使用它:

它使用parse_urlparse_str解析网址和参数,而不是重新整理整个网址,这是更好,更可靠的方法。

$urls[] = 'http://youtu.be/vrXgLhkv21Y';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?t=foobar';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?foo=bar&t=4s';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?t=1h19m14s';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?t=19m14s';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?t=2h';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?t=5m';
$urls[] = 'http://youtu.be/vrXgLhkv21Y?t=50s';

#extract the part after the &t= in urls and run the function if exists 
foreach( $urls as $url ) {

    $parsed_url = parse_url( $url );

    if ( empty( $parsed_url['query'] ) )
        continue;

    parse_str( $parsed_url['query'], $args );

    if ( empty( $args['t'] ) )
        continue;

    echo youtube_time_to_seconds( $args['t'] ) . "\n";
}

输出:

4
4754
1154
7200
300
50

答案 2 :(得分:2)

$url = "http://www.youtube.com/watch?v=TjTbNWhsG28#t=40m&foo=1&bar=2";
$regex_pattern1 = "([#|\&]+t+\=+[0-9]+[m]+[0-9]+[s])"; // minutes and seconds
$regex_pattern2 = "([#|\&]+t+\=+[0-9]+[m])"; // only minutes
$regex_pattern3 = "([#|\&]+t+\=+[0-9]+[s])"; // only seconds
$pattern_used = null;
if(!preg_match_all($regex_pattern1, $url, $time)) // not found "#t=XmXs"
{
    if(!preg_match_all($regex_pattern2, $url, $time)) // not found "#t=Xm"
    {
        if(!preg_match_all($regex_pattern3, $url, $time)) // not found "#t=Xs"
            die("Invalid URL");
        else $pattern_used = 3;
    }   
    else $pattern_used = 2;
}
else $pattern_used = 1;
$time = substr($time[0][0], 3, strlen($time[0][0])); // deleting "#t="

//echo $time; // prints "1m40s" or "40s" if only seconds are given

// --------------
$pattern_minutes = "([0-9]+[s])";
$pattern_seconds = "([0-9]+[m])";
if($pattern_used == 1) // we have both minutes and seconds defined
{
    $minutes = preg_split($pattern_minutes, $time);
    $seconds = preg_split($pattern_seconds, $time);

    $array = array("minutes" => substr($minutes[0], 0, -1), 
                   "seconds" => substr($seconds[1], 0, -1));

    /* we have the following array now:
    array (size=2)
        'minutes' => string '1' (length=1)
        'seconds' => string '40' (length=2)*/
}
else
{
    if($pattern_used == 2) $time2 = preg_split($pattern_minutes, $time);
    else $time2 = preg_split($pattern_seconds, $time); 
    $time2 = substr($time2[0], 0, -1);
    echo $time2; // prints "40"
}