Joao Zanetti Joao Zanetti - 5 days ago 6
PHP Question

Iframe in PHP with string from SQL

I'm trying to convert a YouTube video link to You Tube embed link. A string will receive the YouTube link from SQL, another code will convert to embed link and converted link will be used inside a iframe. I don't know why its not working.

YouTube converter code (I'm trying to use any of these: Link to Embed)

function convertYoutube($string) {
return preg_replace(
"/\s*[a-zA-Z\/\/:\.]*youtu(be.com\/watch\?v=|.be\/)([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i",
"<iframe src=\"//www.youtube.com/embed/$2\" allowfullscreen></iframe>",
$string
);
}


String that receive YouTube link from SQL

$row["link"]

Answer

If you execute this, you will see the embeded youtube video. The $object is the embeded youtube url.

<?php 
$url = "https://www.youtube.com/watch?v=RboEKl7GgU0";
parse_str( parse_url( $url, PHP_URL_QUERY ), $v );
$object = '<iframe width="560" height="315" src="https://www.youtube.com/embed/'.$v['v'].'" frameborder="0" allowfullscreen></iframe>';
echo $object;
?>

Instead of the $url you can obviously just use your $row['link']

Comments