6

This function embed youtube videos if found in a string.

My question is what would be the easiest way to only capture the embedded video (the iframe and only the first if there are more) and ignore rest of the string.

function youtube($string,$autoplay=0,$width=480,$height=390)
{
preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
  return preg_replace(
    '#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_;-]+)#i',
    "<div align=\"center\"><iframe title=\"YouTube video player\" width=\"$width\" height=\"$height\" src=\"http://www.youtube.com/embed/$match[2]?autoplay=$autoplay\" frameborder=\"0\" allowfullscreen></iframe></div>",
    $string);
}
6
  • The easiest and most robust way would be not using a regex. Commented Nov 19, 2011 at 21:04
  • @FailedDev Care to show me how (doesn't have to be the same function)? Commented Nov 19, 2011 at 21:13
  • You are passing part of the html with the $string right? How do you get that string? Commented Nov 19, 2011 at 21:21
  • 2
    I read your question five times, and in my head one question keeps spawning: "What is the input string?" Commented Nov 19, 2011 at 21:22
  • The function is currently used for user posts to automatically embed yt videos. I am trying to use the same function (or logic) to let users use youtube videos as their avatars. Commented Nov 19, 2011 at 21:29

1 Answer 1

16

Okay, I think I see what you're trying to accomplish. A user inputs a block of text (some comment or whatever), and you find a YouTube URL in that text and replace it with the actual video embed code.

Here's how I've modified it:

function youtube($string,$autoplay=0,$width=480,$height=390)
{
    preg_match('#(?:http://)?(?:www\.)?(?:youtube\.com/(?:v/|watch\?v=)|youtu\.be/)([\w-]+)(?:\S+)?#', $string, $match);
    $embed = <<<YOUTUBE
        <div align="center">
            <iframe title="YouTube video player" width="$width" height="$height" src="http://www.youtube.com/embed/$match[1]?autoplay=$autoplay" frameborder="0" allowfullscreen></iframe>
        </div>
YOUTUBE;

    return str_replace($match[0], $embed, $string);
}

Since you're already locating the URL with the first preg_match(), there's no need to run another regex function for replacing it. Have it match the entire URL, then do a simple str_replace() of your entire match ($match[0]). The video code is captured in the first subpattern ($match[1]). I'm using preg_match() because you only want to match the first URL found. You'd have to use preg_match_all() and modify the code a bit if you wanted to match all URLs, not just the first.

Here's an explanation of my regular expression:

(?:http://)?    # optional protocol, non-capturing
(?:www\.)?      # optional "www.", non-capturing
(?:
                # either "youtube.com/v/XXX" or "youtube.com/watch?v=XXX"
  youtube\.com/(?:v/|watch\?v=)
  |
  youtu\.be/     # or a "youtu.be" shortener URL
)
([\w-]+)        # the video code
(?:\S+)?        # optional non-whitespace characters (other URL params)
Sign up to request clarification or add additional context in comments.

3 Comments

I've been searching for this script all over the net. This one works but it only detect the first url in the content. For example, I have 3 youtube urls in the content. The first video will embed while the others just display links. What should I do?
I've got it! Just change preg_match to preg_match_all... Thanks a millions!
To further improve this, add https support too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.