사이트 내 전체검색
유투브 동영장 주소 추출 정규식 예 1
로빈아빠
https://cmd.kr/javascript/861 URL이 복사되었습니다.

본문

A YouTube video URL may be encountered in a variety of formats:

  • latest short format: http://youtu.be/NLqAF9hrVbY
  • iframe: http://www.youtube.com/embed/NLqAF9hrVbY
  • iframe (secure): https://www.youtube.com/embed/NLqAF9hrVbY
  • object param: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
  • object embed: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
  • watch: http://www.youtube.com/watch?v=NLqAF9hrVbY
  • users: http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
  • ytscreeningroom: http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
  • any/thing/goes!: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
  • any/subdomain/too: http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
  • more params: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec

Here is a PHP function with a commented regex that matches each of these URL forms and converts them to links (if they are not links already):

// Linkify youtube URLs which are not already links.function linkifyYouTubeURLs($text) {    $text = preg_replace('~
        # Match non-linked youtube URL in the wild. (Rev:20111012)
        https?://         # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)? # Optional subdomain.
        (?:               # Group host alternatives.
          youtu\.be/      # Either youtu.be,
        | youtube\.com    # or youtube.com followed by
          \S*             # Allow anything up to VIDEO_ID,
          [^\w\-\s]       # but char before ID is non-ID char.
        )                 # End host alternatives.
        ([\w\-]{11})      # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w\-]|$)     # Assert next char is non-ID or EOS.
        (?!               # Assert URL is not pre-linked.
          [?=&+%\w]*      # Allow URL (query) remainder.
          (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
          | </a>          # or inside <a> element text contents.
          )               # End recognized pre-linked alts.
        )                 # End negative lookahead assertion.
        [?=&+%\w-]*        # Consume any URL (query) remainder.
        ~ix', 
        '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',        $text);    return $text;}

And Here is a JavaScript version with the exact same regex (with comments removed):

// Linkify youtube URLs which are not already links.function linkifyYouTubeURLs(text) {    var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['"][^<>]*>|<\/a>))[?=&+%\w-]*/ig;    return text.replace(re,        '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>');}

Notes:

  • The VIDEO_ID portion of the URL is captured in the one and only capture group: $1.
  • If you know that your text does not contain any pre-linked URLs, you can safely remove the negative lookahead assertion which tests for this condition (The assertion beginning with the comment:"Assert URL is not pre-linked.") This will speed up the regex somewhat.
  • The replace string can be modified to suit. The one provided above simply creates a link to the generic "http://www.youtube.com/watch?v=VIDEO_ID" style URL and sets the link text to:"YouTube link: VIDEO_ID".

댓글목록

등록된 댓글이 없습니다.

831 (1/17P)

Search

Copyright © Cmd 명령어 18.191.102.112