【问题标题】:How to fetch youtube embed params from text?如何从文本中获取 youtube 嵌入参数?
【发布时间】:2013-04-14 10:21:19
【问题描述】:

如何使用 PHP 从文本中分解所有 Youtube 嵌入值?例如我有文字:

Fails: <iframe width="560" height="315" src="http://www.youtube.com/embed/Ujwod-vqyqA" frameborder="0" allowfullscreen></iframe>

More fails: <iframe width="560" height="315" src="http://www.youtube.com/embed/DYRTzXSYixQ" frameborder="0" allowfullscreen></iframe>

More the more: <iframe width="560" height="315" src="http://www.youtube.com/embed/uLWqUW_U6dQ" frameborder="0" allowfullscreen></iframe>

我想使用为我的 Facebook 帖子嵌入的所有视频之一,例如第一个视频嵌入:

https://i3.ytimg.com/vi/Ujwod-vqyqA/mqdefault.jpg

我该怎么做?我试过strpos,但这不起作用。

【问题讨论】:

  • 我尝试使用“strpos”,但它只给出了一个数字,嵌入在哪里。我想到了“爆炸”,但我不知道这怎么可能……应该是这样的:获取所有“youtube.com/embed/*”并将其插入数组中,然后我可以从中选择
  • 你的开发方法是什么?发布您尝试过的代码片段会有所帮助。
  • 我不知道该怎么做,这就是我在这里问的原因。就像我说的,我试过了: $youtube_look = "youtube.com/embed"; $youtube_embed = strpos($article, $youtube_look);但它只给了我数字
  • 所以基本上像这样的东西应该可以完成这项工作吗? preg_match_all('/&lt;iframe.*?src="(http(?:s)?:\/\/(?:www.)youtube.com\/embed\/.*?)".*?&gt;/i', file_get_contents('yourfile.txt'), $m); print_r($m[1]);

标签: php


【解决方案1】:
$text = 'Fails: <iframe ...';

foreach(preg_split("/((\r?\n)|(\r\n?))/", $text) as $line) {
    if(!empty($line)) {
        $line = str_replace(array("iframe", "<", ">", '"'), null, $line);
        $values = explode(" ", trim($line));
        foreach($values as $value) {
            if(strpos($value, "=")) {
                $explode = explode("=", $value);
                $thisparams[$explode[0]] = $explode[1];
            }
        }
        $params[] = $thisparams;
    }
}

(Reference for preg_split)

【讨论】:

    猜你喜欢
    • 2012-06-23
    • 2019-02-20
    • 2014-01-16
    • 2015-10-31
    • 2013-09-02
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2012-02-11
    相关资源
    最近更新 更多