2

YoutubeのビデオURLから変数を抽出して埋め込みコードを取得しようとしています。これまでのところ、埋め込みの古い方法は機能していますが、Iframeの方法はありません。URLの「watch?v =」の後に11文字しかない場合、Iframeは正常に機能しますが、URLに「&feature=g」が11文字含まれている場合に問題が発生します。「&feature = g」を取り除く解決策はありますか、それとももっと良い方法がありますか?第二に、それほど重要ではありませんが、自動再生をクリックするたびに、ビデオでautplayの結果が得られませんか?これが私の例です

埋め込みコードを取得するためのPHP

<?php
$vari ='';

if($_POST)
{
    $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;

//Iframe code

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

//Old way to embed code
echo  ('<embed width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash" src="http://www.youtube.com/v/'.$step2[0].'" wmode="transparent" embed="" /></embed>');
}
?>

html

<html>

<form method="post" action="">
URL:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" value="<?php $vari ?>" name="yurl"> <!--$vari-->
<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>
4

1 に答える 1

0

複数回呼び出すのではなく、explode()REGEXを介してvidIDを抽出できます。

$vid_url = "http://www.youtube.com/watch?v=2RgTmJ5oCCA&feature=g-vrec";
preg_match('/(?<=v=)[^&]+/', $vid_url, $vid_id);
//the vid's ID is now in $vid[0]
于 2012-07-11T15:08:31.927 に答える