0

How can I explode all Youtube embed values from a text using PHP? For example I have the text:

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>

And I want to use one of all videos embed for my facebook post, for example first video embed:

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

How can I do this? I tried with strpos, but this does not work.

4

1 回答 1

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;
    }
}

preg_split 的参考

于 2013-04-21T13:41:41.250 回答