$val) { $vals = @explode (";q", $val); $userchoices[$key] = $vals[0]; } if (count ($available) === 0) return $userchoices[0]; // Look at the best existing solution foreach ($userchoices as $choice) { foreach ($available as $avail) { if (strtolower ($avail) === strtolower ($choice)) return $avail; // Case en_US $availTmp = str_replace ("_", "-", $avail); if (strtolower ($availTmp) === strtolower ($choice)) return $avail; // Case text/xml, application/csv (just compare xml or csv) $mimes = explode ("/", $choice); if (isset ($mimes[1]) && strtolower ($availTmp) === $mimes[1]) return $avail; } } // No best solution found. Use the default available solution return $default; } }