I'm really having some difficult wrapping my head around regex and preg_replace. What I want to do is have a function that scans an entire page for any <a href> tags pointing to Youtube and then convert them into an embedded youtube video.

Here's a code to show kinda of what I mean:

<?php
$youtubelink = "http://www.youtube.com/watch?v=Ysl6NCoshk8";
$vid_section = substr( $youtubelink, strpos($youtubelink, '?') + 1, 1 );

$vid_id = substr( $youtubelink, strpos($youtubelink, '=') + 1 );

$embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/'.$vid_section.'/'.$vid_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/'.$vid_section.'/'.$vid_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';

echo $embed_code;
?>

First of all I don't want to be providing the link, I want the function to search for it itself. So here's my stab at regex and preg_replace:

echo preg_replace('#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#', '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$matches[2].'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$matches[2].'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>', $ENTIREPAGE?);

Any idea where I'm going wrong?

To give a little more insight (if your bored enough to care), I have a tumblr blog that posts my twitter RSS feed updates. I sometimes post youtube links and would like them to show up as embedded videos for users to watch on the tumblr page.

Cheers.

Recommended Answers

All 4 Replies

If you are talking about grabbing the youtube links from a page the following scripts will do that and display each video id in a list.

<?php
$input=file_get_contents('http://www.youtube.com/');
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
foreach ($output[3] AS $video_id) {
    echo $video_id.'<br>';
    }
foreach ($output[4] AS $video_id) {
    echo $video_id.'<br>';
    }
?>

Awesome. Worked like a charm. Here's the final code I got:

<?php
$input=file_get_contents('http://warrendunlop.tumblr.com');
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
foreach ($output[4] AS $video_id) {
$embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
echo $embed_code.'<br>';
    }
?>

I am having a couple new problems. It seems to embedding doubles for each link on the blog page. Any way to check for a twin link? (you can see what I mean on my test page: www.warrendunlop.com/youtube_embed.php

I also am not sure how I would scan the current document instead of an external one.

To only do each video id once the following is what the code would look like.

<?php
$input=file_get_contents('http://warrendunlop.tumblr.com');
preg_match_all('#(http://www.youtube.com)?/(v/([-|~_0-9A-Za-z]+)|watch\?v\=([-|~_0-9A-Za-z]+)&?.*?)#i',$input,$output);
foreach ($output[4] AS $video_id) {
    if (!isset($video[$video_id])) {
    $video[$video_id]=true;
$embed_code = '<object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>';
echo $embed_code.'<br>';
        }
    }
?>

You are a legend sir.

Much thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.