PHP/Regex:从youtubeurl中提取视频id-嵌入代码


PHP/Regex: extracting video id from youtube url- embedded code

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

<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>');
?>

也许我在这里遗漏了一些东西,但使用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'];

请参阅代码板上的示例。

当然,如果你的输入是youtube提供的完整代码片段,你需要一个DOM解析器来获取url。

您似乎忘记了在HTML中使用"echo"来输出变量。如果您将内联PHP更改为:,它会起作用吗

<?php echo $text ?>

和:

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

最后一个我把它改成了把两根绳子剪在一起,它们似乎试图做同样的事情,所以也许你只需要其中一根,但如果你想把它们都印在一起,我把它变成的应该可以。

这有帮助吗?