从字符串中解析YouTube ID



我将以下字符串作为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除外,字符串都是相同的。我在编写正则表达式方面很糟糕,任何人可以救我吗?

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

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

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

最新更新