gpt4 book ai didi

php - 交换所有 youtube 网址以通过 preg_replace() 嵌入

转载 作者:可可西里 更新时间:2023-11-01 00:35:09 25 4
gpt4 key购买 nike

您好,我正在尝试将 youtube 链接转换为嵌入代码。

这是我的:

<?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;
?>

它适用于一个链接。但是,如果我添加两个,它只会交换最后一次出现的位置。我需要更改什么?

最佳答案

您没有正确处理字符串的末尾。删除 $ , 并将其替换为结束标记 </a> .这将解决它。

 $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);

关于php - 交换所有 youtube 网址以通过 preg_replace() 嵌入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10435645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com