我有一个html文字包含YouTube链接,我尝试直接在同一页面上替换这些链接并显示视频但是我的代码中有错误(视频没有出现),我也想要修改模式来查找所有youtube将youtube.com
youtu.be
链接在一起
我的代码:
$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: