1

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
    check answer [here](http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string) – Darvex Jul 11 '12 at 15:06
  • @Darvex, +1 for the initial help. I am new to php. If you could post that answer in application to my question, I will be happy to click accept. Thank you. –  Jul 11 '12 at 15:48

1 Answers1

0

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]
Mitya
  • 33,629
  • 9
  • 60
  • 107