public function get_best_mime($mime_types = null)
{
// Values will be stored in this array
$accept_types = array();
// Accept header is case insensitive, and whitespace isn’t important
$accept = strtolower(str_replace(' ', '', $_SERVER['HTTP_ACCEPT']));
// divide it into parts in the place of a ","
$accept = explode(',', $accept);
foreach ($accept as $a) {
// the default quality is 1.
$q = 1;
// check if there is a different quality
if (strpos($a, ';q=')) {
// divide "mime/type;q=X" into two parts: "mime/type" and "X"
list($a, $q) = explode(';q=', $a);
}
// mime-type $a is accepted with the quality $q
// WARNING: $q == 0 means, that mime-type isn’t supported!
$accept_types[$a] = $q;
}
arsort($accept_types);
// if no parameter was passed, just return parsed data
if (!$mime_types) {
return $accept_types;
}
$mime_types = array_map('strtolower', (array) $mime_types);
// let's check our supported types:
foreach ($accept_types as $mime => $q) {
if ($q && in_array($mime, $mime_types)) {
return $mime;
}
}
// no mime-type found
return null;
}