Page 1 of 1

remove &feature in youtube URL

Posted: Wed Jan 11, 2012 12:09 pm
by Fonis
hey guys, i have this php code, that i took from a guide on how to embed youtube URLs automatically on my website. The problem is though that the code only needs two thirds of the URL and therefore leaves the rest as normal text (it's from &feature to the end in the URL). Is it possible to add something to the script so it removes the URL-text, or does i have to write a comment above telling the users to manually delete it?

Code: Select all

$text=stripslashes($_POST['content']);

function show_youtube($text)
{
    $VID_WID = 320;
    $VID_HEI = 240;
 
    for ($k=0; $k<9; $k++)
    {
        $text .= ' ';
        $find = 'youtube.com/watch?v=';
        $pos = strpos($text, $find);
        if ($pos === false)
        {
            break;
        }
        $len = strlen($text);
        for ($i=$pos; $i>=0; $i--)
        {
            if (substr($text, $i, 6) == 'http:/')
            {
                $pos1 = $i;
                break;
            }
        }
        for ($i=$pos; $i<$len; $i++)
        {
            if (in_array($text[$i], array('&', ' ', "\r", "\n", ',', "\t")))
            {
                $pos2 = $i;
                break;
            }
        }
        $link1 = substr($text, $pos1, $pos2 - $pos1);
        $link2 = str_replace('/watch?v=', '/v/', $link1);
 
        $embed =    '<object width="' . $VID_WID . '" height="' . $VID_HEI . '">'.
                    '<param name="movie" value="' . $link2 . '"></param>'.
                    '<param name="allowFullScreen" value="true"></param>'.
                    '<param name="allowscriptaccess" value="always"></param>'.
                    '<embed src="' . $link2 . '" type="application/x-shockwave-flash" '.
                    'allowscriptaccess="always" allowfullscreen="true" '.
                    'width="' . $VID_WID . '" height="' . $VID_HEI . '"></embed></object>';
 
        $text = str_replace($link1, $embed, $text);
    }
    return $text;
}

Re: remove &feature in youtube URL

Posted: Wed Jan 11, 2012 12:51 pm
by twinedev
What exactly will $_POST['content'] contain? Will it be a bunch of other content with a full URL to youtube in the middle of it, or just be the URL only?

Re: remove &feature in youtube URL

Posted: Wed Jan 11, 2012 1:08 pm
by Fonis
a bunch of different content

Re: remove &feature in youtube URL

Posted: Wed Jan 11, 2012 1:43 pm
by twinedev
Try the following function instead. Unlike the other code you gave, this one also will work with the youtube short url format: http://youtu.be/ABCDEFG

Code: Select all

function show_youtube($text) {
    $VID_WID = 320;
    $VID_HEI = 240;

    if (preg_match_all('%(https?://(www\.)?youtube\.com/watch[^\s]*?(\?|&)v=([a-z0-9_-]+)[^\s]*)|(http://(www\.)?youtu\.be/([a-z0-9_-]+))%i',$text,$regs,PREG_SET_ORDER)) {

        $embed = '<object width="' . $VID_WID . '" height="' . $VID_HEI . '">'.
        '<param name="movie" value="http://www.youtube.com/v/~~VIDEO_ID~~"></param>'.
        '<param name="allowFullScreen" value="true"></param>'.
        '<param name="allowscriptaccess" value="always"></param>'.
        '<embed src="http://www.youtube.com/v/~~VIDEO_ID~~" type="application/x-shockwave-flash" '.
        'allowscriptaccess="always" allowfullscreen="true" '.
        'width="' . $VID_WID . '" height="' . $VID_HEI . '"></embed></object>';

        foreach($regs as $aryParts) {
            $strVidID = (isset($aryParts[7])) ? $aryParts[7] : $aryParts[4];
            $text = str_replace($aryParts[0],str_replace('~~VIDEO_ID~~',$strVidID,$embed),$text);
        }
    }
    return $text;
}

Re: remove &feature in youtube URL

Posted: Wed Jan 11, 2012 2:10 pm
by Fonis
thank you, the code worked perfect :)

Re: remove &feature in youtube URL

Posted: Wed Jan 11, 2012 2:24 pm
by twinedev
It was funny, the code you originally posted remind me on how I used to write things before learning Regex.

There was also a 3rd variation of the youtube code, but it was mainly when you were logged into your own youtube account and viewing your own videos. Had a client that found that for me when I only programmed it for the two formats I just did here. (I had the admin set that if they posted a video link, and it didn't match, it would e-mail me.)

-Greg