【问题标题】:embed first only youtube video首先嵌入 youtube 视频
【发布时间】:2012-05-08 09:20:44
【问题描述】:

我正在使用以下功能将 youtube 链接显示为视频,但问题是我不知道要替换多个视频,所以如何将其限制为仅 1 个?

php代码:

function embedYoutube($text)
{

    $pattern = '/[\\?\\&]v=([^\\?\\&]+)/';
    $replacement = '<div style="width:100%;float:left;margin-top:15px;margin-bottom:15px;"><iframe width="570" height="315" src=http://www.youtube.com/embed/$1 frameborder="0" allowfullscreen></iframe></div>';
    return preg_replace($pattern, $replacement, $text);

}

【问题讨论】:

    标签: php regex preg-replace pattern-matching embed


    【解决方案1】:
    return preg_replace($pattern, $replace, $text, 1);
    

    preg_replace的第四个参数是替换的限制。

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2012-06-25
      • 1970-01-01
      • 2020-08-25
      • 2015-06-03
      • 2014-07-07
      • 2019-05-05
      • 1970-01-01
      相关资源
      最近更新 更多