I have a function that parses a YouTube URL to retrieve the video id and then it generates a clean embed code for most use cases. The embed breaks if I pass it a URL where time_continue exists such as https://www.youtube.com/watch?time_continue=79&v=2uBDNhe6Gkk&feature=emb_title
if(isset($_POST['submit'])){
$embedURL = mysqli_real_escape_string($con, $_POST['embedCode']);
function convertYoutube($string) {
return preg_replace(
"'#(?:https?:\/\/)?(?:m\.|www\.)?(?:youtu\.be\/|youtube\-nocookie\.com\/embed\/|youtube\.com\/(?:embed\/|v\/|e\/|\?v=|shared\?ci=|watch\?v=|watch\?.+&v=))([-_A-Za-z0-9]{10}[AEIMQUYcgkosw048])\S*#s'",
"\https://www.youtube.com/embed/$2",
$string
);
}
$embedURL = convertYoutube($embedURL);
$embedCode = "<div class=''embed-responsive embed-responsive-16by9''><iframe class=''embed-responsive-item'' src=''$embedURL'' allowfullscreen></iframe></div>";
}
The code produces the following (it doesn't strip the time_continue and feature=emb_title parameters which I need removed).
<div class='embed-responsive embed-responsive-16by9'><iframe class='embed-responsive-item' src='https://www.youtube.com/watch?time_continue=79&v=2uBDNhe6Gkk&feature=emb_title' allowfullscreen></iframe></div>
I'd like the function to format it like embed code below without parameters. Thank you
<div class='embed-responsive embed-responsive-16by9'><iframe class='embed-responsive-item' src='https://www.youtube.com/embed/2uBDNhe6Gkk' allowfullscreen></iframe></div>