Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an example function that uses a regular expression to extract the youtube ID from a URL:</p> <pre><code>/** * get youtube video ID from URL * * @param string $url * @return string Youtube video id or FALSE if none found. */ function youtube_id_from_url($url) { $pattern = '%^# Match any youtube URL (?:https?://)? # Optional scheme. Either http or https (?:www\.)? # Optional www subdomain (?: # Group host alternatives youtu\.be/ # Either youtu.be, | youtube\.com # or youtube.com (?: # Group path alternatives /embed/ # Either /embed/ | /v/ # or /v/ | /watch\?v= # or /watch\?v= ) # End path alternatives. ) # End host alternatives. ([\w-]{10,12}) # Allow 10-12 for 11 char youtube id. $%x' ; $result = preg_match($pattern, $url, $matches); if ($result) { return $matches[1]; } return false; } echo youtube_id_from_url('http://youtu.be/NLqAF9hrVbY'); # NLqAF9hrVbY </code></pre> <p>It's an adoption <a href="https://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string/5831191#5831191">of the answer from a similar question</a>.</p> <hr> <p>It's not directly the API you're looking for but probably helpful. Youtube has an <a href="http://oembed.com/" rel="nofollow noreferrer">oembed</a> service:</p> <pre><code>$url = 'http://youtu.be/NLqAF9hrVbY'; var_dump(json_decode(file_get_contents(sprintf('http://www.youtube.com/oembed?url=%s&amp;format=json', urlencode($url))))); </code></pre> <p>Which provides some more meta-information about the URL:</p> <pre><code>object(stdClass)#1 (13) { ["provider_url"]=&gt; string(23) "http://www.youtube.com/" ["title"]=&gt; string(63) "Hang Gliding: 3 Flights in 8 Days at Northside Point of the Mtn" ["html"]=&gt; string(411) "&lt;object width="425" height="344"&gt;&lt;param name="movie" value="http://www.youtube.com/v/NLqAF9hrVbY?version=3"&gt;&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/NLqAF9hrVbY?version=3" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&gt;" ["author_name"]=&gt; string(11) "widgewunner" ["height"]=&gt; int(344) ["thumbnail_width"]=&gt; int(480) ["width"]=&gt; int(425) ["version"]=&gt; string(3) "1.0" ["author_url"]=&gt; string(39) "http://www.youtube.com/user/widgewunner" ["provider_name"]=&gt; string(7) "YouTube" ["thumbnail_url"]=&gt; string(48) "http://i3.ytimg.com/vi/NLqAF9hrVbY/hqdefault.jpg" ["type"]=&gt; string(5) "video" ["thumbnail_height"]=&gt; int(360) } </code></pre> <p>But the ID is not a direct part of the response. However it might contain the information you're looking for and it might be useful to validate the youtube URL.</p>
 

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