An implementation of the WHATWG Mime Sniffing specification
Find a file
2024-12-27 07:26:16 -05:00
lib Remove sniff pattern constants from MimeType class 2020-04-30 12:27:05 -04:00
tests Update for PHP 8.4 2024-12-27 07:26:16 -05:00
vendor-bin Update for PHP 8.4 2024-12-27 07:26:16 -05:00
.gitattributes Initial export from Lax 2020-04-16 14:44:36 -04:00
.gitignore Initial export from Lax 2020-04-16 14:44:36 -04:00
.php_cs.dist Initial export from Lax 2020-04-16 14:44:36 -04:00
AUTHORS Initial export from Lax 2020-04-16 14:44:36 -04:00
CHANGELOG Update changelog 2021-03-06 22:58:00 -05:00
composer.json Update for PHP 8.4 2024-12-27 07:26:16 -05:00
composer.lock Update for PHP 8.4 2024-12-27 07:26:16 -05:00
LICENSE Initial export from Lax 2020-04-16 14:44:36 -04:00
README.md Document MIME type group feature 2020-04-19 12:07:41 -04:00
robo Initial export from Lax 2020-04-16 14:44:36 -04:00
robo.bat Initial export from Lax 2020-04-16 14:44:36 -04:00
RoboFile.php Initial export from Lax 2020-04-16 14:44:36 -04:00

MIME Sniffing

This library aims to be a complete implementation of the WHATWG Mime Sniffing specification. Presently it only implements MIME type parsing (in other words, MIME sniffing itself is not implemented), but it will be expanded in due course.

Features

Parsing

A MIME type string may be parsed into a structured MimeType instance as follows:

$mimeType = \MensBeam\Mime\MimeType::parse("text/HTML; charSet=UTF-8");
echo $mimeType->type;              // prints "text"
echo $mimeType->subtype;           // prints "html"
echo $mimeType->essence;           // prints "text/html"
echo $mimeType->params['charset']; // prints "UTF-8"

Normalizing

Once parsed, a MimeType instance can be serialized to produce a normalized text representation:

$typeString = 'TeXt/HTML;  CHARset="UTF\-8"; charset=iso-8859-1; unset='; 
$mimeType = \MensBeam\Mime\MimeType::parse($typeString);
echo (string) $mimeType; // prints "text/html;charset=UTF-8"

MIME type groups

The MIME Sniffing specification defines a series of MIME type groups; these are exposed via the boolean properties isArchive, isAudioVideo, isFont, isHtml, isImage, isJavascript, isJson, isScriptable, isXml, and isZipBased. For example:

$mimeType = \MensBeam\Mime\MimeType::parse("image/svg+xml");
var_export($mimeType->isImage);      // prints "true"
var_export($mimeType->isXml);        // prints "true"
var_export($mimeType->isScriptable); // prints "true"
var_export($mimeType->isArchive);    // prints "false"