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 3225628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:21:41+00:00 2026-05-17T16:21:41+00:00

I want to replace {youtube}Video_ID_Here{/youtube} with the embed code for a youtube video. So

  • 0

I want to replace

{youtube}Video_ID_Here{/youtube}

with the embed code for a youtube video.

So far I have

preg_replace('/{youtube}(.*){\/youtube}/iU',...)

and it works just fine.

But now I’d like to be able to interpret parameters like height, width, etc. So could I have one regex for this whether is does or doesn’t have parameters? It should be able to inperpret all of these below…

{youtube height="200px" width="150px" color1="#eee" color2="rgba(0,0,0,0.5)"}Video_ID_Here{/youtube}

{youtube height="200px"}Video_ID_Here{/youtube}

{youtube}Video_ID_Here{/youtube}

{youtube width="150px" showborder="1"}Video_ID_Here{/youtube}

  • 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-17T16:21:42+00:00Added an answer on May 17, 2026 at 4:21 pm

    Try this:

    function createEmbed($videoID, $params)
    {
        // $videoID contains the videoID between {youtube}...{/youtube}
        // $params is an array of key value pairs such as height => 200px
    
        return 'HTML...'; // embed code
    }
    
    if (preg_match_all('/\{youtube(.*?)\}(.+?)\{\/youtube\}/', $string, $matches)) {
        foreach ($matches[0] as $index => $youtubeTag) {
            $params = array();
    
            // break out the attributes
            if (preg_match_all('/\s([a-z0-9]+)="([^\s]+?)"/', $matches[1][$index], $rawParams)) {
                for ($x = 0; $x < count($rawParams[0]); $x++) {
                    $params[$rawParams[1][$x]] = $rawParams[2][$x];
                }
            }
    
            // replace {youtube}...{/youtube} with embed code
            $string = str_replace($youtubeTag, createEmbed($matches[2][$index], $params), $string);
        }
    }
    

    this code matches the {youtube}…{/youtube} tags first and then splits out the attributes into an array, passing both them (as key/value pairs) and the video ID to a function. Just fill in the function definition to make it validate the params you want to support and build up the appropriate HTML code.

    • 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 want to replace url with links, the following code work fine but it
I want to replace Demo image by code css3 or somthing else. I have
I have an embeded youtube video which I want the YouTube API applied. I
I have people inserting video code to be displayed on my website. I want
I want to replace backslash => '\' with secure &#92; replacement. But my code
I have this code, and I want to know, if I can replace only
If you want to embed youtube dynamically then it is totally easy. You just
I want to replace some headers by images, so I would have nice font.For
I want to replace a regex with '*', but only if the regex is

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.