【问题标题】:php regex get first youtube id form textphp regex 获取第一个 youtube id 表单文本
【发布时间】:2014-03-10 16:03:29
【问题描述】:

如何获得:

WA4nNDfQ0wA

形成这个代码:

<p>some text here ...</p>
<iframe width="640" height="360" src="//www.youtube.com/embed/WA4nNDfQ0wA?feature=player_embedded" frameborder="0" allowfullscreen></iframe>
<div>some text here ...</div>
<iframe width="640" height="360" src="//www.youtube.com/embed/9gypDWA6r2s?feature=player_embedded" frameborder="0" allowfullscreen></iframe>

通过使用 php?

【问题讨论】:

    标签: php regex youtube


    【解决方案1】:

    假设您的 html 在 $html 变量中。

    preg_match("|.*?/embed/([\w-]+)|", $html, $m);
    print $m[1];
    

    如果您将.*? 更改为.*(greedy),那么它将匹配最后一个 id。

    【讨论】:

    • 非常感谢。这就像一个魅力。我怎样才能学习正则表达式,你能给我一个很好的学习链接吗?
    • 你可以找到很多使用谷歌学习正则表达式的选项。但是,您可以查看 [this site][1] 以获得正则表达式帮助。您可以使用 [this site][2] 来获取任何正则表达式的解释。 [1]:regular-expressions.info [2]:rick.measham.id.au/paste/explain.pl
    • 我总是发现proftpd documentation 有一个非常适合新手的正则表达式介绍。
    猜你喜欢
    • 2011-03-24
    • 2015-03-27
    • 1970-01-01
    • 2012-04-15
    • 2016-04-22
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多