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 關注
- 146 瀏覽
添加回答
舉報