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.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
baitas212123
  • 151
  • 1
  • 4
  • 14
  • i tried with "strpos" but it given only a number, where is embed. I think about "explode", but i dont know how it is possible.. it should be like this: get all "youtube.com/embed/*" and insert it in array and then i will be available to select on of them – baitas212123 Apr 21 '13 at 13:15
  • What were your dev approaches? Posting your tried sniplet would help. – Gottlieb Notschnabel Apr 21 '13 at 13:18
  • i have no idea how to do ir, thats why i am asking here. Like as i said, i tried with it: $youtube_look = "http://www.youtube.com/embed/"; $youtube_embed = strpos($article, $youtube_look); but it gaven me only number – baitas212123 Apr 21 '13 at 13:19
  • So basically something like this should do the job ? `preg_match_all('//i', file_get_contents('yourfile.txt'), $m); print_r($m[1]);` – HamZa Apr 21 '13 at 13:22
  • Take look into [preg_match_all](http://php.net/preg_match_all) – Gabor Garami Apr 21 '13 at 13:24
  • yes i think so, but why you use *? and *?> ? – baitas212123 Apr 21 '13 at 13:30
  • @user2304363 `.*` -> match everything, when you add a `?` to it (`.*?`) you will make it ungreedy (please search for this as there are so many examples/tutorials). – HamZa Apr 21 '13 at 13:38

1 Answers1

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)

Community
  • 1
  • 1
Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116