【问题标题】:Swap all youtube urls to embed via preg_replace()通过 preg_replace() 交换所有 youtube url 以嵌入
【发布时间】:2012-05-13 05:16:37
【问题描述】:

您好,我正在尝试将 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;
?>

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

【问题讨论】:

    标签: php preg-replace


    【解决方案1】:

    试试这个:preg_replace($search, $replace, $text, -1);

    我知道这是默认设置,但谁知道...

    编辑如果不起作用,请尝试;

    do{
        $text = preg_replace($search, $replace, $text, -1, $Count);
    }
    while($Count);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢!我一定在所有这些正则表达式符号中都错过了它。
      • 当然,你也错过了正则表达式域中贪婪的含义。
      【解决方案3】:

      这里有一个更有效的正则表达式:http://pregcopy.com/exp/26,将其翻译为 PHP:(添加“s”修饰符)

      <?php
      
      $text = $post->text;
      
           $search = '#<a (?:.*?)href=["\\\']http[s]?:\/\/(?:[^\.]+\.)*youtube\.com\/(?:v\/|watch\?(?:.*?\&)?v=|embed\/)([\w\-\_]+)["\\\']#ixs';
           $replace = '<center><iframe width="560" height="315" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe></center>';
      
           $text = preg_replace($search, $replace, $text);
      
      
      echo $text;
      ?>
      

      测试一下

      【讨论】:

        【解决方案4】:

        一个视频有两种类型的 youtube 链接:

        例子:

        $link1 = 'https://www.youtube.com/watch?v=NVcpJZJ60Ao';
        $link2 = 'https://www.youtu.be/NVcpJZJ60Ao';
        

        此函数同时处理:

        function getYoutubeEmbedUrl($url)
        {
             $shortUrlRegex = '/youtu.be\/([a-zA-Z0-9_-]+)\??/i';
        $longUrlRegex = '/youtube.com\/((?:embed)|(?:watch))((?:\?v\=)|(?:\/))([a-zA-Z0-9_-]+)/i';
        
            if (preg_match($longUrlRegex, $url, $matches)) {
                $youtube_id = $matches[count($matches) - 1];
            }
        
            if (preg_match($shortUrlRegex, $url, $matches)) {
                $youtube_id = $matches[count($matches) - 1];
            }
            return 'https://www.youtube.com/embed/' . $youtube_id ;
        }
        

        $link1 或 $link2 的输出是一样的:

         $output1 = getYoutubeEmbedUrl($link1);
         $output2 = getYoutubeEmbedUrl($link2);
         // output for both:  https://www.youtube.com/embed/NVcpJZJ60Ao
        

        现在您可以在 iframe 中使用输出了!

        【讨论】:

        • @Toto 感谢您的建议,我从网站上复制了这个正则表达式,不知道$matches[count($matches) - 1] 是什么意思!!但我认为它可能是这样的:$matches[0] 不是这个:$matches[-1] 对吗?因为我们将有一个匹配项,它将是$matches 数组中的第一个匹配项。
        • 对不起,我混合了其他语言,其中$matches[-1]代表最后一个索引,在PHP中我们必须使用end($matches)。这里我们在数组中有 3 个元素。请不要在未经测试的情况下从外部站点复制/粘贴代码。至少提供参考。
        猜你喜欢
        • 1970-01-01
        • 2012-04-11
        • 2018-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-16
        • 2021-09-06
        • 2019-06-23
        相关资源
        最近更新 更多