douzhang2092 2012-07-11 15:02
浏览 22
已采纳

PHP:来自youtube网址的嵌入式代码 - 工作但是iframe问题

I am trying to obtain embeded code through extracting variables from Youtube video URLs. So far I have the old way of embedding working but not the Iframe way. The Iframe works fine if the url only has the 11 characters after "watch?v=" but the issue comes when the url contains "&feature=g" the 11 characters. Is there a solution to strip off the "&feature=g" or maybe a better way? Second and less important, every time i click autoplay i dont obtain an autplay result with video? Here is my EXAMPLE

PHP for obtaining embeded code

<?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>
  • 写回答

1条回答 默认 最新

  • douzhangkui2467 2012-07-11 15:08
    关注

    Rather than multiple explode() calls you can extract the vid ID via REGEX.

    $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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?