0

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"]

1 Answer 1

1

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']

Sign up to request clarification or add additional context in comments.

9 Comments

@JoaoZanetti I have tested this code, and it works fine exactly like it above. It either has to do with the URL you are feeding it or something else. Please provide more information about how to url looks like if still doesn't work.
I don't know what is going wrong for you then, I have tried it with that link and any other, I get a great result.
In chrome its white, in Vivaldi its showing "This plugin is not supported."
I have updated my code, see if this is working better for you. I have tested this too, and it works just fine ( It looks a lot better as well ). @JoaoZanetti
@JoaoZanetti Please let me know if this has solved your problem!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.