Gets an encoding from a given string.
Description
This is an algorithm defined in the WHAT-WG specification.
Example:
'UTF-8' === self::get_encoding( 'utf8' );
'UTF-8' === self::get_encoding( " \tUTF-8 " );
null === self::get_encoding( 'UTF-7' );
null === self::get_encoding( 'utf8; charset=' );
See also
Parameters
$labelstringrequired- A string which may specify a known encoding.
Source
protected static function get_encoding( string $label ): ?string {
/*
* > Remove any leading and trailing ASCII whitespace from label.
*/
$label = trim( $label, " \t\f\r\n" );
/*
* > If label is an ASCII case-insensitive match for any of the labels listed in the
* > table below, then return the corresponding encoding; otherwise return failure.
*/
switch ( strtolower( $label ) ) {
case 'unicode-1-1-utf-8':
case 'unicode11utf8':
case 'unicode20utf8':
case 'utf-8':
case 'utf8':
case 'x-unicode20utf8':
return 'UTF-8';
default:
return null;
}
}
Changelog
| Version | Description |
|---|---|
| 6.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.