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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:40:01+00:00 2026-06-07T11:40:01+00:00

The function purpose is to validate the URLs of a YouTube video and check

  • 0

The function purpose is to validate the URLs of a YouTube video and check if the video exists. This is a snippet of my actual code. I manipulate the string to my desired format and then i proceed to check if it is valid and exists. If it passes the test, then i echo the results. The problem is that I am not calling the function correctly.

I am getting this echo even though the video does exist:

The video does not exist or invalid url

Edited: and added isValidURL function
*Code for checking if video exist or is invalid:*

 if($_POST)
    {

            // After applying url manipulation and getting the url in a proper format result = $formatted_url


function isValidURL($formatted_url) {

            $formatted_url = trim($formatted_url);
            $isValid = true;

            if (strpos($formatted_url, 'http://') === false && strpos($formatted_url, 'https://') === false) {
                $formatted_url = 'http://'.$formatted_url;
            }

            //first check with php's FILTER_VALIDATE_URL
            if (filter_var($formatted_url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === false) {
                $isValid = false;
            } else {
                //not all invalid URLs are caught by FILTER_VALIDATE_URL
                //use our own mechanism

                $host = parse_url($formatted_url, PHP_URL_HOST);
                $dotcount = substr_count($host, '.');

                //the host should contain at least one dot
                if ($dotcount > 0) {
                    //if the host contains one dot
                    if ($dotcount == 1) {
                        //and it start with www.
                        if (strpos($host, 'www.') === 0) {
                            //there is no top level domain, so it is invalid
                            $isValid = false;
                        }
                    } else {
                        //the host contains multiple dots
                        if (strpos($host, '..') !== false) {
                            //dots can't be next to each other, so it is invalid
                            $isValid = false;
                        }
                    }
                } else {
                    //no dots, so it is invalid
                    $isValid = false;
                }
            }

            //return false if host is invalid
            //otherwise return true
            return $isValid;
        }

        $isValid = getYoutubeVideoID($formatted_url);


            function isYoutubeVideo($formatted_url) {
                $isValid = false;
                    //validate the url, see: http://snipplr.com/view/50618/
                if (isValidURL($formatted_url)) {
                            //code adapted from Moridin: http://snipplr.com/view/19232/                
                    $idLength = 11;
                    $idOffset = 3;
                    $idStarts = strpos($formatted_url, "?v=");
                    if ($idStarts !== FALSE) {
                                    //there is a videoID present, now validate it
                        $videoID = substr($formatted_url, $idStarts + $idOffset, $idLength);
                        $http = new HTTP("http://gdata.youtube.com");
                        $result = $http->doRequest("/feeds/api/videos/".$videoID, "GET");
                                    //returns Array('headers' => Array(), 'body' => String);
                        $code = $result['headers']['http_code'];
                                    //did the request return a http code of 2xx?
                        if (substr($code, 0, 1) == 2) {
                            $isValid = true;
                        }
                    }
                }
                return $isValid;
            }

            $isValid = isYoutubeVideo($formatted_url);
            parse_str($parsed_url['query'], $parsed_query_string);
            $v = $parsed_query_string['v'];

            if ( $isValid == true ) {
                //Iframe code 
                echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$v.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');

                //Old way to embed code
                echo htmlentities ('<embed src="http://www.youtube.com/v/'.$v.'" width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash"  wmode="transparent" embed="" /></embed>');
                }
            else {

            echo ("The video does not exist or invalid url"); 

            }


    }
    ?>
  • 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-07T11:40:04+00:00Added an answer on June 7, 2026 at 11:40 am

    You are missing the isValidURL() function. Try changing this line:

    if (isValidURL($formatted_url)) {   
    

    to

    if(preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $formatted_url, $result)) {
    

    or

    $test = parse_url($formatted_url);
    if($test['host']=="www.youtube.com"){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Purpose of my code is: - Validate the form and check for empty fields
Best Guess: method - def(self, maybeSomeVariables); lines of code which achieve some purpose Function
I have this function in my model the purpose of it is to get
I have a function that is calling wget via subprocess.Popen. The purpose of this
Does the following code render the using(...) function/purpose irrelevant? Would it cause a deficiency
Here is my function: # Purpose: Scrape Floraweb.de for plant species data (photograph, sociology,
What is the purpose of the std::remquo function? What is an example of when
Possible Duplicate: What is the purpose of a self executing function in javascript? Please,
What is the purpose of putting an 'N' in front of function parameters in
I'm having a problem with my function. The purpose of my function is to

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.