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

  • Home
  • SEARCH
  • 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 8210869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:13:41+00:00 2026-06-07T10:13:41+00:00

I am currently trying to retrieve YouTube video IDs through url. I created a

  • 0

I am currently trying to retrieve YouTube video IDs through url. I created a function getYoutubeVideoID that strips $url and finds the $sVideoID. The Problem is that when i echo the variable i get an empty value of $sVideoID. How come i am getting no value result for $sVideoID if i am assigning it the video ID?

<?php

if($_POST)
{

$url     = $_POST['yurl'];

function getYoutubeVideoID($url) {
    $sVideoID = preg_replace('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/| youtube\.com\S*[^\w\-\s])([\w\-]{11})      
        (?=[^\w\-]|$)(?![?=&+%\w]*(?:[\'"][^<>]*>| </a>))[?=&+%\w-]*~ix','<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',$url);
    return $sVideoID;
}

    $hth        = 300; //$_POST['yheight'];
    $wdth       = 500; //$_POST['ywidth'];


?>

<?
//Iframe code

echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$sVideoID.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');

?>

<?
//Old way to embed code
echo htmlentities ('<embed src="http://www.youtube.com/v/'.$sVideoID.'" width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash"  wmode="transparent" embed="" /></embed>');
}
?>
  • 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-06-07T10:13:43+00:00Added an answer on June 7, 2026 at 10:13 am

    Perhaps I am wrong, or maybe you haven’t included all relevant data, but I do not see anywhere in your code where you execute the function you have created.

    Instead of what you are currently doing, try this:

    echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.getYoutubeVideoID($url).'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
    

    You can’t simply define a function and it will execute and assign variables.. what you have has the function, when it is executed, return a variable in it’s place. As an example, you could assign $var the return value of your function:

    $var = getYoutubeVideoID($url);
    

    Instead of the very above, you could even try this:

    $sVideoID = getYoutubeVideoID($url);
    

    Lastly, instead of using regex as others have noted, you could use parse_url() in combination with parse_str() to get the data from your url:

    $arr = parse_url($url);
    parse_str($arr['query'], $output);
    echo $output['v'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to build a little widget that will retrieve a list
I'm currently trying to retrieve the coordinates of the cursor using function getCursor(event) {
Using C# i'm trying to retrieve the name of the song that is currently
I am currently trying to parse an HTML document to retrieve all of the
Currently trying at add ajax to a site, after much reading I discovered that
I am currently trying to write some code that will accept some FTP details,
I'm trying to retrieve a listing of a user's youtube videos and embed them
I'm currently trying to move through all the values added to a listbox by
I'm trying to retrieve the access level (admin/member/guest) for the currently logged in user
I'm currently trying to map a java ArrayList with a Flex ArrayCollection, through LCDS.

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.