baitas212123
baitas212123

Reputation: 151

How to fetch youtube embed params from text?

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.

Upvotes: 0

Views: 149

Answers (1)

dialogik
dialogik

Reputation: 9552

$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)

Upvotes: 1

Related Questions