Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:00:37+00:00 2026-05-27T00:00:37+00:00

This function embed youtube videos if found in a string. My question is what

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:00:38+00:00Added an answer on May 27, 2026 at 12:00 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: var post = $('.post'); post.find('.video embed').each(function(){ v = $(this); youtubeID = v.attr('src').replace(/^.*youtube.com.*(?:\/|v=)(\w+)\//g,);
I use this code to let users embed youtube videos on a website i
I'm building a page where my Youtube video channel videos will be displayed. This
This function appears to be a way to access all sorts of system values.
Is this function declaration in C#: void foo(string mystring) the same as this one
Example: /** * This function will determine whether or not one string starts with
I'm trying to retrieve a listing of a user's youtube videos and embed them
Im looking for a function that will pull the youtube duration of a video
I would like to insert a youtube video and play it right away, I
I have a slider which includes 4 youtube videos that are embedded via the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.