1 回答
TA贡献2036条经验 获得超8个赞
试试这个正则表达式
(https:\/\/www\.youtube\.com\/.*?)(?:\s|$)|(https:\/\/youtu\.be\/.*?)(?:\s|$)
例
用法:
$re = '/(https:\/\/www\.youtube\.com\/.*?)(?:\s|$)|(https:\/\/youtu\.be\/.*?)(?:\s|$)/';
$str = 'abc https://www.youtube.com/watch?v=Dx5qFachd3A def https://youtu.be/Dx5qFachd3A ghi';
$embedStart = '<iframe width="560" height="315" src="';
$embedEnd = '" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
$str = preg_replace($re,$embedStart.'$0'.$embedEnd,$str);
echo $str;
您可以使用preg_replace将其替换为嵌入代码。
- 1 回答
- 0 关注
- 169 浏览
添加回答
举报
