How to get a link to the still image of a YouTube video?

YouTube has multiple servers for serving images of the videos, hosted on the site. They are mainly used for load ballancing (so you get the image that is hosted on a server, that is physically closest to you so it loads faster).

However, as a developer or a website owner you may want to programmatically build a link to the thumbnail or still image that YouTube uses for a given video.

What you need is – the ID of the video (e.g. if the video in question is http://www.youtube.com/watch?v=m3JAtvsqfF8, the ID would be m3JAtvsqfF8). When you have the ID of the video, just replace it in the below URL and open it in your browser.

http://img.youtube.com/vi/video_id_here/0.jpg

You should immediately see the full-size thumbnail that YouTube uses across its website (note that scaled down versions are also available; just replace the last zero in the URL with “1.jpg”, “2.jpg” or “hqdefault.jpg”).


Counting the Number of Options/Elements in a <SELECT> Dropdown with jQuery

Let’s say you have this (very) simple <select> dropdown:


And one day you decide to figure out how much elements or <option> tags the given <select> has.

It’s easy:

var items = $('#my_dropdown option').length;
alert(items);

The above should immediately display “2″ since we have 2 countries in the select dropdown.


Find Value of Key in Associative Array

I recently stumbled upon the following issue – I had an associative array with key=>value type and I wanted to get the value of a specific key. I wasn’t able to find a suitable built-in PHP function, so I decided to write something of my own. Feel free to reuse it however you like.

Include this function anywhere in your PHP file:

function find_by_key($searched, $array){
	if(!is_array($array)) return FALSE; // We haven't passed a valid array
 
	foreach($array as $key=>$value){
		if($key==$searched) return $value; // Match found, return value
	}
	return FALSE; // Nothing was found
}

Then you can use it by following this example:

$sample_array = array(
	'bmw'=>'fast',
	'mercedes'=>'luxury',
	'audi'=>'expensive'
);
 
echo find_by_key('audi', $sample_array);

This example should output ‘expensive’. I hope you find this useful.


Check If Viewed Through Mobile

On many occasions, you may want to check if the current visitor is browsing your site through a mobile browser. There are a lot of solutions out there (classes, libraries), but the simplest option has been working fine for me for years.

<?php
    $iphone = (bool) strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
    $ipad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
    $android = (bool) strpos($_SERVER['HTTP_USER_AGENT'],"Android");
    $webos = (bool) strpos($_SERVER['HTTP_USER_AGENT'],"WebOS");
 
if(!$ipad){
    echo 'Not on iPad';
}else{
    echo 'On iPad';
}
// Etc...
?>

Pages:12345678