Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are many, many questions on SO about regexping Youtube video IDs - just do a Google or site search. I took the liberty to modify <a href="https://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string">this answer by ridgerunner</a> to do what you want, ie. replace a Youtube URL with the embed code. Have a look and edit the pattern or embed code if needed. For example, you might want to wrap the embedded video in a <code>div</code>.</p> <pre><code>&lt;?php // Replace Youtube URLs with embed code function embedYoutube($text) { $search = '~ # Match non-linked youtube URL in the wild. (Rev:20130823) (?:https?://)? # Optional scheme. (?:[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'; $replace = '&lt;object width="425" height="344"&gt; &lt;param name="movie" value="http://www.youtube.com/v/$1?fs=1"&lt;/param&gt; &lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt; &lt;param name="allowScriptAccess" value="always"&gt;&lt;/param&gt; &lt;embed src="http://www.youtube.com/v/$1?fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" width="425" height="344"&gt; &lt;/embed&gt; &lt;/object&gt;'; return preg_replace($search, $replace, $text); } $string = 'This is the forum post content with some Youtube links:'."\n". 'http://www.youtube.com/watch?v=NLqAF9hrVbY'."\n". 'http://www.youtube.com/v/u1zgFlCw8Aw?fs=1&amp;hl=en_US'; echo embedYoutube($string); ?&gt; </code></pre>
 

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