在 PHP 字符串中查找 youtube 链接并将其转换为嵌入代码?

2022-01-20 00:00:00 string youtube find replace php

在PHP字符串中找到一个Youtube视频链接并将其转换成嵌入代码强>?

Find a Youtube video link in PHP String and convert it into Embed Code?

嵌入代码:

<iframe width="420" height="315" src="//www.youtube.com/embed/0GfCP5CWHO0" frameborder="0" allowfullscreen></iframe>

PHP 代码/字符串:

<?php echo $post_details['description']; ?>

Youtube 链接:

http://www.youtube.com/watch?v=0GfCP5CWHO0

推荐答案

试试这个:

preg_replace("/s*[a-zA-Z//:.]*youtube.com/watch?v=([a-zA-Z0-9-_]+)([a-zA-Z0-9/*-\_?&;\%=.]*)/i","<iframe width="420" height="315" src="//www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>",$post_details['description']);

相关文章