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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测