Convert a youtube video url to embed code

0
434
convert youtube url to embed url php

welcome back to shortlearner.com , in our previous post we learn how to Convert words to numbers in PHP. in this post we will see how to Convert a youtube video url to embed code with the help of PHP.
so in the below code we create a PHP function convertYoutube and pass YouTube link as a parameter, the convertYoutube function is converts our URL into embed code.

convert youtube url to embed url php

Also Read :
PHP Login Script With Remember me.
Unable to create a directory a wordpress error
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
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 width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/$2\" allowfullscreen></iframe>",
		$string
	);
}
$text = "https://www.youtube.com/watch?v=74WSXAFXoDk";
echo convertYoutube($text); ?>