I'm retriving a youtube link from mysql db with this code

<?php
//echo $embed;
$yt_url=$embed;
function get_youtube_id_from_url($url)
    {
    if (stristr($url,'youtu.be/'))
        { preg_match('/(https|http):\/\/(.*?)\/([a-zA-Z0-9_]{11})/i', $url, $final_ID);return $final_ID[3]; }
    else 
        { preg_match('/(https|http):\/\/(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-zA-Z0-9_]{11})/i', $url, $IDD); return $IDD[5]; }
    }
$variable= get_youtube_id_from_url($yt_url);
?>

and extracting the id part of the link and placing it in this piece of code below

echo "<div align='center'><iframe src=\"//www.youtube.com/embed/" . $variable. "\" style=\"background: #fff;\" frameborder=\"0\" height=\"300\" scrolling=\"auto\" width=\"480\"></iframe></div>";

but am told the vedio dose not exit, please help

Dani AI

Generated

The "video does not exist" message almost always means the embedded ID is wrong (or the video itself is removed/private/restricted). Common root causes in this thread: the stored DB value contains an HTML anchor or encoded entities, the extraction regex missed the hyphen (as noted), or the extracted ID has extra characters (quotes, ampersand-fragments, whitespace). supplied a robust regex that handles many URL forms, and confirmed it works — those are good options, but a parsing-first approach is usually clearer and easier to debug long-term.

Diagnostic checklist and approach

  • Inspect the raw DB value (show raw string with var_dump) to see if it is a plain URL, an <a> tag, or already an embed snippet.
  • Strip HTML and decode entities before parsing (strip_tags + html_entity_decode) so ampersands and hrefs don’t break parsing.
  • Prefer parse_url + parse_str to get the v query parameter for standard watch URLs; for youtu.be links, take the path segment after the slash; use a short, safe cleanup pass to remove stray characters.
  • Always verify the candidate ID is exactly 11 characters after cleanup; if not, echo the value and re-check the original string.
  • Echo the final iframe src (or open it directly in a browser) to confirm the constructed URL matches expectations. If the ID is correct but YouTube still says the video is gone, the video may be removed, private, or region-restricted.

Minimal PHP extraction pattern (conceptual)

$raw = trim(html_entity_decode(strip_tags($embed)));
$parts = parse_url($raw);
if (!empty($parts['query'])) { parse_str($parts['query'], $q); $id = $q['v'] ?? null; }
if (empty($id) && !empty($parts['host']) && strpos($parts['host'],'youtu.be')!==false) { $id = ltrim($parts['path'],'/'); }
if (empty($id)) { $segments = explode('/', trim($parts['path'] ?? $raw,'/')); $id = end($segments); }
$id = preg_replace('/[^\w-]/','',$id);  // keep letters, digits, underscore, dash
if (strlen($id)!==11) { /* invalid id: log/debug */ }

Longer-term: store the cleaned 11-character ID in the database (not an HTML snippet), prefer https in the iframe src, and consider validating IDs with the YouTube Data API if availability checks are required.

Recommended Answers

All 3 Replies

The pattern in the preg match is missing the dash -, it should be [a-zA-Z0-9_-], so change the first to:

'/(https|http):\/\/(.*?)\/([a-zA-Z0-9_-]{11})/i'

And the second to:

'/(https|http):\/\/(.*?)\/(embed\/|watch\?v=|(.*?)&v=|v\/|e\/|.+\/|watch.*v=|)([a-zA-Z0-9_-]{11})/i'

Because in case of links as $yt_url = "http://youtu.be/--9oAhOSwpg"; it will not work.

Try this

<?php
$yt_url="http://www.youtube.com/watch?v=ZvzwthHh-IQ";
function get_youtube_id_from_url($url)
    {
        preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
        return $matches[1];
    }
$variable= get_youtube_id_from_url($yt_url);

echo "<div align='center'><iframe src=\"//www.youtube.com/embed/" . $variable. "\" style=\"background: #fff;\" frameborder=\"0\" height=\"300\" scrolling=\"auto\" width=\"480\"></iframe></div>";
?>
commented: This solved my problem before I asked +2

@Bachov Varghese code works like a charm :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.