7

In a system that I'm developing I need to recognize the youtube link in the following format

[youtube]youtube url[/youtube]

for the moment I arrived at this regular expression:

#\[youtube\]http://www.youtube\.(.*)/watch\?v=([a-zA-Z0-9_-]*)\[\/youtube\]#s

But this pattern isn't able to recognize url like

[youtube]http://www.youtube.com/watch?v=s3wXkv1VW54&feature=fvst[/youtube]

Note the feature=fvst.

Some one can help me in recognize all possible youtube url?

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
Luca Bernardi
  • 4,191
  • 2
  • 31
  • 39
  • 1
    Related: http://stackoverflow.com/questions/1658763/php-regex-youtube, http://stackoverflow.com/questions/1773822/get-youtube-video-id-from-html-code-with-php - see http://stackoverflow.com/search?q=youtube+regex – Gordon Feb 08 '10 at 10:11

2 Answers2

4

How about

|\[youtube\]http://www.youtube\.(.*?)/watch\?v=([a-zA-Z0-9_-]*)[%&=#a-zA-Z0-9_-]*\[\/youtube\]|s

EDIT: Changed the regex to keep only the video id inside parentheses.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • If two "youtube-links" are on the same line, the greedy `.*` will mess things up. I'd put a reluctant `?` after it, as Andy suggested (**edit:** who has now deleted his answer...). – Bart Kiers Feb 08 '10 at 10:13
  • No Tim, you failed: I saw you had a `.*` without a `?`! Guilty as charged! :) +1 – Bart Kiers Feb 08 '10 at 10:16
  • Your pattern works but i like to directly match the video ID, with your pattern I have the entire query string. – Luca Bernardi Feb 08 '10 at 10:18
  • @Bart: Misunderstanding there :) I meant that I saw Andy's answer in time before he removed it. I did only change my regex after your comment. – Tim Pietzcker Feb 08 '10 at 10:20
  • I deleted my answer because Tim pointed out that it wasn't quite the problem, and I didn't want to be hit by the downvote brigade. It's bad enough getting downvoted when you give the correct answer ;-) – Andy E Feb 08 '10 at 10:25
  • @Andy: True, it was not the answer to the question, but you had a point, hence my comment. – Bart Kiers Feb 08 '10 at 10:28
  • This SO answer provides an alternative : http://stackoverflow.com/a/12814235/363573 – Stephan Oct 10 '12 at 07:39
1

Notes:
I'd perform a case-insensitive match on URLs (/i)
. matches anything; use \. instead to match the URL
Also, "www." in Youtube URLs is optional.
Use (\.[a-z]+){1,2} instead of ".*" to match ".com", ".co.uk", .. after "youtube"

Assuming the "&feature" part is optional, the regex would be:

/\[youtube\]http:\/\/(www\.)?youtube(\.[a-z]+){1,2}\/watch\?v=([a-z0-9_-]*)(&feature=([a-z]+))?\[\/youtube\]/is

Assuming the "&feature" part is optional, AND there can be other parameters than "feature":

/\[youtube\]http:\/\/(www\.)?youtube(\.[a-z]+){1,2}\/watch\?v=([a-z0-9_-]*)(&([a-z]+)=([a-z0-9_-])+)*?\[\/youtube\]/is
We Know
  • 95
  • 1
  • 3