i have a html text Contain YouTube links and i try to Replace these links and displayed videos directly on same page But there is something wrong in my code (Videos does not appear) , I also want modified pattern to looking for all youtube links youtube.com
youtu.be
together
my code :
$text = '<div id="post_message_395">
<a href="https://www.youtube.com/watch?v=FM0mkgv2JWk" target="_blank">https://www.youtube.com/watch?v=FM0mkgv2JWk</a><br>
<br>
Bla Bla Bla Bla Bla Bla
<br>
<a href="https://youtu.be/FM0mkgv2JWk" target="_blank">https://youtu.be/FM0mkgv2JWk</a><br>
<br>
Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla
</div>';
$reg_exUrl = '#(?:<a[^\<\>]+href=\")?(?:http://)?(?:[a-zA-Z]{1,4}\.)?youtube.com/watch\?v=(.{11}?)[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:</a>)?#';
preg_match_all($reg_exUrl, $text, $matches);
$links = $matches[2];
foreach ($links as $val)
{
$turl= '<object width="425" height="350">
<param name="movie" value="'.$val.'&rel=0"></param>
<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>
<embed src="'.$val.'&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" allownetworking="internal" width="580" height="420">
</embed>
</object>';
$text = str_replace($val,$turl,$text);
}
echo $text;
outpot :