noresources / ns-php-mediatype
Media Type parsing and manipulation library
v2.0.1
2025-05-31 13:32 UTC
Requires
- php: >= 7.1
- noresources/http-core: ^2.0
Requires (Dev)
- ext-dom: *
- phpunit/phpunit: >= 5.0
Replaces
- dev-main / 2.0.x-dev
- v2.0.1
- v2.0.0
- 1.7.x-dev
- dev-master / 1.7.x-dev
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- 1.6.x-dev
- v1.6.0
- 1.5.x-dev
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- 1.4.x-dev
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- 1.2.x-dev
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- 1.0.x-dev
- dev-test
This package is auto-updated.
Last update: 2025-05-31 13:34:10 UTC
README
RFC 6838 Media Type (MIME type) parsing and comparison.
Installation
composer require noresources/mediatype
Usage
use NoreSources\MediaType\MediaType; use NoreSources\MediaType\MediaTypeFactory; use NoreSources\MediaType\MediaRange; $factory = MediaTypeFactory::getInstance(); $mediaType = $factory->createFromString('text/vnd.noresources.incredibly.flexible+xml'); var_dump($mediaType->getMainType()); // "text" var_dump($mediaType->getStructuredSyntax()); // "xml" $subType = $mediaType->getSubType(); var_dump(\strval($subType)); // "vnd.noresources.incredibly.flexible+xml" var_dump($subType->getFacets()); // [ "vnd", "noresources", "incredibly", "flexible" ] // From a file or a stream $mediaType = $factory->createFromMedia('path/to/filename.html'); var_dump(\strval($mediaType)); // "text/html" // Media range is also recognized $range = $factory->createFromString('image/*'); // Comparing $html = $factory->createFromString('text/html'); $anyText = $factory->createFromString('text/*'); $any = $factory->createFromString('*/*'); var_dump([ 'text/html vs text/*' => MediaRange::compare($html, $anyText), 'text/* vs */*' => MediaRange::compare($anyText, $any), '*/* vs text/html' => MediaRange::compare($any, $html) ]); /* array(3) { ["text/html vs text/*"]=> int(1) ["text/* vs */*"]=> int(1) ["*/* vs text/html"]=> int(-1) } */