【问题标题】:How can I replace a soundcloud url to an iFrame (PHP)如何将 soundcloud url 替换为 iFrame (PHP)
【发布时间】:2015-05-09 04:32:38
【问题描述】:

如何使用 PHP 检测并用 iframe 替换文本中的 soundcloud url:

例如:

这个:

https://soundcloud.com/s/eminem-ft-dr-dre-old-time-sake

进入这个:

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/140068709&amp;color=00aabb&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe>

【问题讨论】:

    标签: php url iframe preg-replace


    【解决方案1】:
      function linkifySoundcloudURLs( $text )
      {
        $text = preg_replace('#https{0,1}:\/\/w{0,3}\.*soundcloud\.com\/([^< ]+)#ix',
        '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https://soundcloud.com/$1&amp;color=00aabb&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false"></iframe>',
          $text);
        return $text;
      }
    

    为我工作。

    【讨论】:

      【解决方案2】:

      我不确定我是否理解正确,但是像这样吗?

      <?php
        $url = "https://soundcloud.com/aathmigan/eminem-ft-dr-dre-old-time-sake";
      ?>    
      
      <iframe width="100%" height="166" scrolling="no" frameborder="no"
          src="<?php echo $url; ?>"></iframe>
      

      【讨论】:

      • 我不确定您为什么要使用preg_replace。为什么不使用ifelse ifelse 设置一个url 变量呢?帮助我理解、解释它的用途以及应该如何使用它。
      • 我需要一个带有preg replace的函数,因为它应该首先检测文本中的链接,然后替换链接
      • If-else 会容易得多,但如果你真的需要preg_replace,请使用this link
      • This link 非常适合你想玩正则表达式。
      猜你喜欢
      • 1970-01-01
      • 2016-08-17
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多