0

I have this regex patter to grab YouTube video ID's

preg_match_all( '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i', $string, $res );

However it doesn't seem to work properly with the string in the example bellow:

<?php

$string = "yet, another youtube video.

[url=http://www.youtube.com/watch?feature=player_embedded&v=6kVpM5WK5JI#]http://www.youtube.com/watch?feature=player_embedded&v=6kVpM5WK5JI#[/url]


Fonte:onothersite";

preg_match_all( '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i', $string, $res );

print_r($res);

?>

The code above outputs this:

Array (
  [0] =>
    Array (
      [0] => youtube.com/watch?feature=player_embedded&v=6kVpM5WK5JI#]http://www.youtube.com/watch?feature=player_embedded&v=6kVpM5WK5JI#[/url] Font
    )
  [1] => 
    Array ( 
      [0] => url] Font
    )
)

And should output this:

Array (
  [0] =>
    Array (
      [0] => youtube.com/watch?v=6kVpM5WK5JI
    )
  [1] => 
    Array ( 
      [0] => 6kVpM5WK5JI 
    )
)

Any idea how to solve this?

Thanks.

Highly Irregular
  • 38,000
  • 12
  • 52
  • 70
BornKillaz
  • 592
  • 7
  • 19
  • You could consider matching a generic URL (that starts with `youtube.com` or `youtu.be`) and then doing the extraction of the actual parameters using [`parse_url`](http://php.net/manual/en/function.parse-url.php) and [`parse_str`](http://uk3.php.net/manual/en/function.parse-str.php). – Martin Ender Dec 11 '12 at 20:03
  • Does this help?: http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string/5831191#5831191 – Highly Irregular Dec 11 '12 at 20:07
  • Also see: http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match – Highly Irregular Dec 11 '12 at 20:08
  • Another thing: YouTube has more TLDs than just *.com -- you might wanna consider allowing more than just *.com, depending on what this is for. – Ingo Bürk Dec 11 '12 at 20:10
  • @HighlyIrregular Thank you very much, the first link did the trick! m.buettner, thanks, but parse_url cannot be used to match all youtube URL's (some of them have no v=xxxxxxxxxxx) IngoBürk, thanks, however, it seams that all posted youtube links on our community end either with the .com or .be TDL. – BornKillaz Dec 12 '12 at 11:31

1 Answers1

1

Watch out for greedy patterns like .+ and .*! I replaced it with [^\/]+ and [^#]* respectively and it seems to work.

This

preg_match_all( '/(?:youtube\.com\/(?:[^\/]+\/[^\/]+\/|(?:v|e(?:mbed)?)\/|[^#]*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i', $string, $res );

Gives

Array ( 
    [0] => Array ( 
        [0] => youtube.com/watch?feature=player_embedded&v=6kVpM5WK5JI 
        [1] => youtube.com/watch?feature=player_embedded&v=6kVpM5WK5JI 
        ) 
    [1] => Array ( 
        [0] => 6kVpM5WK5JI 
        [1] => 6kVpM5WK5JI 
        ) 
    ) 

Updated to remove second greedy pattern.

Ranty
  • 3,333
  • 3
  • 22
  • 24
  • Thank you very much for your answer and concern. It did work, however someone suggested this: http://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string/5831191#5831191 and I found it was much more complete than the example I was using before, so I will be using that regex pattern. Thanks once again. – BornKillaz Dec 12 '12 at 11:37