【问题标题】:PHP/Regex: extracting video id from youtube url- embedded codePHP/Regex:从 youtube url-嵌入式代码中提取视频 ID
【发布时间】:2012-07-11 16:39:20
【问题描述】:

我正在尝试从 youtube url 输入中获取嵌入式代码。我遇到了这个ANSWER,但现在我将这个答案应用到我的代码中,我没有得到任何结果。我的目标是让正则表达式将网址变成这样:http://www.youtube.com/watch?v=videoid。我怎样才能让正则表达式函数真正与我的代码一起工作?

<html>
    <form method="post" action="">
    URL:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" value="<?php $text ?>" name="yurl"> <!--$vari-->
    <br>
    <!--<br>
    Height:&nbsp;
    <input type="text" value="<?php $hth ?>" name="yheight"> $hth
    <br>-->
    <!--<br>
    Width:&nbsp;&nbsp;
    <input type="text" value="<?php $wdth ?>" name="ywidth"> $wdth
    <br>
    <br>-->
    Autoplay:&nbsp;
    <input type="checkbox" value="<?php $auto1; $auto2; ?>" name="yautop">  <!--$auto1 $auto2-->
    <br>
    <input type="submit" value="Generate Embed Code" name="ysubmit">
    <br>
    </form>
</html>

PHP

<?php


$vari ='';

if($_POST)
{



    function linkifyYouTubeURLs($vari) {
        $vari = preg_replace('~
            # Match non-linked youtube URL in the wild. (Rev:20111012)
            https?://         # Required scheme. Either http or https.
            (?:[0-9A-Z-]+\.)? # Optional subdomain.
            (?:               # Group host alternatives.
              youtu\.be/      # Either youtu.be,
            | youtube\.com    # or youtube.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', 
            '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
            $vari);
        return $vari;
    }



    $vari       = $_POST['yurl'];
    $hth        = 300; //$_POST['yheight'];
    $wdth       = 500; //$_POST['ywidth'];
    $is_auto    =   0;

    $step1 =explode ('v=', $vari);
    $step2 =explode ('&amp;',$step1[1]);

    if(isset($_POST['yautop'] ))
    {
        if($_POST['yautop'] == 1)
            $is_auto    =   1;
    }
    $auto1  = '?autoplay='.$is_auto;
    $auto2  = '&autoplay='.$is_auto;
?>

<p>Iframe Result:</p>   
<?
//Iframe code with optional autoplay

echo  ('<iframe src="http://www.youtube.com/embed/'.$step2[0].'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
?>

【问题讨论】:

    标签: php regex


    【解决方案1】:

    也许我在这里遗漏了一些东西,但使用parse_urlparse_str 会不会容易得多:

    $url = 'http://www.youtube.com/watch?v=Imh0vEnOMXU&feature=g-vrec';    // some youtube url
    $parsed_url = parse_url($url);
    /*
     * Do some checks on components if necessary
     */
    parse_str($parsed_url['query'], $parsed_query_string);
    $v = $parsed_query_string['v'];
    

    请参阅example on codepad

    当然,如果您输入的是 sn-p youtube 提供的完整代码,您将需要一个 DOM 解析器来获取该 url。

    【讨论】:

    • +1 不,你没有错过任何东西,我是错过这个重要信息的人。但是如果用户在输入框中输入这样的链接:http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo 会发生什么
    • @charlieCodex 我不知道这种表示法,但是当$v 出现空白时,您需要一个备用方法。 $parsed_url['fragment'] 将包含哈希标记之后的所有内容,因此您可以检查 explode on / 并获取最后一个数组元素,但您需要检查其他可能的 url 是否有效。
    【解决方案2】:

    您似乎忘记在 HTML 中使用“echo”来输出变量。如果将内联 PHP 更改为:

    <?php echo $text ?>
    

    和:

    <?php echo $auto1 . $auto2; ?>
    

    最后一个我把它改成把两条线剪在一起,它们似乎试图做同样的事情,所以也许你只需要其中一个,但如果你想让它们两个一个接一个地打印,我改变了什么into 应该可以解决这个问题。

    这有帮助吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-24
      • 2011-07-02
      • 2012-01-04
      • 2012-03-24
      • 1970-01-01
      • 2016-04-22
      • 2017-02-08
      • 2016-01-19
      相关资源
      最近更新 更多