如何从文本中获取youtube embed params?

时间:2013-04-21 13:11:18

标签: php

如何使用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,但这不起作用。

1 个答案:

答案 0 :(得分: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