user1434156
user1434156

Reputation:

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

I am trying to get embedded code from a youtube url input. I came upon this ANSWER but now that i applied that answer to my code i get no results. My goal is to have the regex make the url into this: http://www.youtube.com/watch?v=videoid. How would i be able to get the regex function to actually work with my code?

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

Upvotes: 3

Views: 2310

Answers (2)

jeroen
jeroen

Reputation: 91734

Maybe I am missing something here but would it not be a lot easier to use parse_url and parse_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'];

See the example on codepad.

Of course if your input it the complete code snippet youtube provides, you would need a DOM parser to get to the url.

Upvotes: 1

SilverSnake
SilverSnake

Reputation: 572

You seem to have forgotten to use "echo" in your HTML to output the variables. Would it work if you changed the inline PHP to:

<?php echo $text ?>

and:

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

This last one I changed it to cut the two strings together, they seem to try and do the same thing so maybe you only need one of them, but if you want both of them printed after each other, what I changed it into should work for that.

Does that help?

Upvotes: 0

Related Questions