Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A YouTube video URL may be encountered in a variety of formats:</p> <ul> <li>latest short format: <code>http://youtu.be/NLqAF9hrVbY</code></li> <li>iframe: <code>http://www.youtube.com/embed/NLqAF9hrVbY</code></li> <li>iframe (secure): <code>https://www.youtube.com/embed/NLqAF9hrVbY</code></li> <li>object param: <code>http://www.youtube.com/v/NLqAF9hrVbY?fs=1&amp;hl=en_US</code></li> <li>object embed: <code>http://www.youtube.com/v/NLqAF9hrVbY?fs=1&amp;hl=en_US</code></li> <li>watch: <code>http://www.youtube.com/watch?v=NLqAF9hrVbY</code></li> <li>users: <code>http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo</code></li> <li>ytscreeningroom: <code>http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I</code></li> <li>any/thing/goes!: <code>http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4</code></li> <li>any/subdomain/too: <code>http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY</code></li> <li>more params: <code>http://www.youtube.com/watch?v=spDj54kf-vY&amp;feature=g-vrec</code></li> <li>query may have dot: <code>http://www.youtube.com/watch?v=spDj54kf-vY&amp;feature=youtu.be</code></li> <li>nocookie domain: <code>http://www.youtube-nocookie.com</code></li> </ul> <p>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):</p> <pre class="lang-php prettyprint-override"><code>// Linkify youtube URLs which are not already links. function linkifyYouTubeURLs($text) { $text = preg_replace('~(?#!js YouTubeId Rev:20160125_1800) # Match non-linked youtube URL in the wild. (Rev:20130823) https?:// # Required scheme. Either http or https. (?:[0-9A-Z-]+\.)? # Optional subdomain. (?: # Group host alternatives. youtu\.be/ # Either youtu.be, | youtube # or youtube.com or (?:-nocookie)? # youtube-nocookie.com \.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. [?=&amp;+%\w.-]* # Allow URL (query) remainder. (?: # Group pre-linked alternatives. [\'"][^&lt;&gt;]*&gt; # Either inside a start tag, | &lt;/a&gt; # or inside &lt;a&gt; element text contents. ) # End recognized pre-linked alts. ) # End negative lookahead assertion. [?=&amp;+%\w.-]* # Consume any URL (query) remainder. ~ix', '&lt;a href="http://www.youtube.com/watch?v=$1"&gt;YouTube link: $1&lt;/a&gt;', $text); return $text; } </code></pre> <p>; // End $YouTubeId.</p> <p>And here is a JavaScript version with the exact same regex (with comments removed):</p> <pre class="lang-js prettyprint-override"><code>// Linkify youtube URLs which are not already links. function linkifyYouTubeURLs(text) { var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&amp;+%\w.-]*(?:['"][^&lt;&gt;]*&gt;|&lt;\/a&gt;))[?=&amp;+%\w.-]*/ig; return text.replace(re, '&lt;a href="http://www.youtube.com/watch?v=$1"&gt;YouTube link: $1&lt;/a&gt;'); } </code></pre> <p><strong>Notes:</strong></p> <ul> <li>The VIDEO_ID portion of the URL is captured in the one and only capture group: <code>$1</code>.</li> <li>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: <em>"Assert URL is not pre-linked."</em>) This will speed up the regex somewhat.</li> <li>The replace string can be modified to suit. The one provided above simply creates a link to the generic <code>"http://www.youtube.com/watch?v=VIDEO_ID"</code> style URL and sets the link text to: <code>"YouTube link: VIDEO_ID"</code>.</li> </ul> <hr> <p><strong>Edit 2011-07-05:</strong> Added <code>-</code> hyphen to ID char class</p> <p><strong>Edit 2011-07-17:</strong> Fixed regex to consume any remaining part (e.g. <em>query</em>) of URL following YouTube ID. Added <code>'i'</code> <em>ignore-case</em> modifier. Renamed function to camelCase. Improved pre-linked lookahead test.</p> <p><strong>Edit 2011-07-27:</strong> Added new "user" and "ytscreeningroom" formats of YouTube URLs.</p> <p><strong>Edit 2011-08-02:</strong> Simplified/generalized to handle new "any/thing/goes" YouTube URLs.</p> <p><strong>Edit 2011-08-25:</strong> Several modifications:</p> <ul> <li>Added a Javascript version of: <code>linkifyYouTubeURLs()</code> function.</li> <li>Previous version had the scheme (HTTP protocol) part optional and thus would match invalid URLs. Made the scheme part required.</li> <li>Previous version used the <code>\b</code> word boundary anchor around the VIDEO_ID. However, this will not work if the VIDEO_ID begins or ends with a <code>-</code> dash. Fixed so that it handles this condition.</li> <li>Changed the VIDEO_ID expression so that it must be exactly 11 characters long.</li> <li>The previous version failed to exclude pre-linked URLs if they had a query string following the VIDEO_ID. Improved the negative lookahead assertion to fix this.</li> <li>Added <code>+</code> and <code>%</code> to character class matching query string.</li> <li>Changed PHP version regex delimiter from: <code>%</code> to a: <code>~</code>.</li> <li>Added a "Notes" section with some handy notes.</li> </ul> <p><strong>Edit 2011-10-12:</strong> YouTube URL host part may now have any subdomain (not just <code>www.</code>).</p> <p><strong>Edit 2012-05-01:</strong> The consume URL section may now allow for '-'.</p> <p><strong>Edit 2013-08-23:</strong> Added additional format provided by @Mei. (The query part may have a <code>.</code> dot.</p> <p><strong>Edit 2013-11-30:</strong> Added additional format provided by @CRONUS: <code>youtube-nocookie.com</code>.</p> <p><strong>Edit 2016-01-25:</strong> Fixed regex to handle error case provided by CRONUS.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload