【问题标题】:Parse youtube ID from string [duplicate]从字符串解析 youtube ID [重复]
【发布时间】:2017-02-22 00:08:06
【问题描述】:

我有以下字符串作为 PHP 变量。

<div class="entry-content-asset">
     <div class="embed">
      <iframe width="500" height="281" src="https://www.youtube.com/embed/DSP_yxvRZOA?feature=oembed" frameborder="0" allowfullscreen></iframe>
    </div>
</div>

我需要以编程方式搜索此字符串并仅找到视频 ID DSP_yxvRZOA

每次保存 youtube 视频 ID 时,字符串都是相同的。我写正则表达式很糟糕,有人能救我吗?

【问题讨论】:

    标签: php regex


    【解决方案1】:

    假设你的字符串被命名为$str:

    $re = '/embed\/(.*)\?feature/';
    preg_match_all($re, $str, $matches);
    

    将匹配embed/?feature 之间的任何内容并将其放入第一个捕获组。

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 2011-05-25
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      相关资源
      最近更新 更多