交换所有YouTube网址以通过preg_replace()嵌入 [英] Swap all youtube urls to embed via preg_replace()
本文介绍了交换所有YouTube网址以通过preg_replace()嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在尝试将youtube链接转换为嵌入代码.
Hello I'm trying to convert youtube links into embed code.
这就是我所拥有的:
<?php
$text = $post->text;
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*$#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
echo $text;
?>
它适用于一个链接.但是,如果我加两个,它将只交换最后一次出现的时间.我必须更改什么?
It works for one link. However if I add two, it will only swap the last occurrence. What do I have to change?
推荐答案
您没有正确处理字符串的结尾.删除$
,并用结束标签</a>
代替.这将解决它.
You're not handling the end of the string properly. Remove the $
, and replace it with the closing tag </a>
. this will fix it.
$search = '#<a(.*?)(?:href="https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*<\/a>#x';
$replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$2" frameborder="0" allowfullscreen></iframe></center>';
$text = preg_replace($search, $replace, $text);
这篇关于交换所有YouTube网址以通过preg_replace()嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文