note 126853 added to function.mb-convert-encoding

From: Date: Tue, 15 Feb 2022 10:00:09 +0000
Subject: note 126853 added to function.mb-convert-encoding
Groups: php.notes 
Request: Send a blank email to php-notes+get-217617@lists.php.net to get a copy of this message
/** * Convert Windows-1250 to UTF-8 * Based on https://www.php.net/manual/en/function.mb-convert-encoding.php#112547 */ class TextConverter { private const ENCODING_TO = 'UTF-8'; private const ENCODING_FROM = 'ISO-8859-2'; private array $mapChrChr = [ 0x8A => 0xA9, 0x8C => 0xA6, 0x8D => 0xAB, 0x8E => 0xAE, 0x8F => 0xAC, 0x9C => 0xB6, 0x9D => 0xBB, 0xA1 => 0xB7, 0xA5 => 0xA1, 0xBC => 0xA5, 0x9F => 0xBC, 0xB9 => 0xB1, 0x9A => 0xB9, 0xBE => 0xB5, 0x9E => 0xBE ]; private array $mapChrString = [ 0x80 => '€', 0x82 => '‚', 0x84 => '„', 0x85 => '…', 0x86 => '†', 0x87 => '‡', 0x89 => '‰', 0x8B => '‹', 0x91 => '‘', 0x92 => '’', 0x93 => '“', 0x94 => '”', 0x95 => '•', 0x96 => '–', 0x97 => '—', 0x99 => '™', 0x9B => '’', 0xA6 => '¦', 0xA9 => '©', 0xAB => '«', 0xAE => '®', 0xB1 => '±', 0xB5 => 'µ', 0xB6 => '¶', 0xB7 => '·', 0xBB => '»' ]; /** * @param $text * @return string */ public function execute($text): string { $map = $this->prepareMap(); return html_entity_decode( mb_convert_encoding(strtr($text, $map), self::ENCODING_TO, self::ENCODING_FROM), ENT_QUOTES, self::ENCODING_TO ); } /** * @return array */ private function prepareMap(): array { $maps[] = $this->arrayMapAssoc(function ($k, $v) { return [chr($k), chr($v)]; }, $this->mapChrChr); $maps[] = $this->arrayMapAssoc(function ($k, $v) { return [chr($k), $v]; }, $this->mapChrString); return array_merge([], ...$maps); } /** * @param callable $function * @param array $array * @return array */ private function arrayMapAssoc(callable $function, array $array): array { return array_column( array_map( $function, array_keys($array), $array ), 1, 0 ); } } ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 178.19.186.5) ---- Manual Page -- https://php.net/manual/en/function.mb-convert-encoding.php Edit -- https://main.php.net/note/edit/126853 Del: integrated -- https://main.php.net/note/delete/126853/integrated Del: useless -- https://main.php.net/note/delete/126853/useless Del: bad code -- https://main.php.net/note/delete/126853/bad+code Del: spam -- https://main.php.net/note/delete/126853/spam Del: non-english -- https://main.php.net/note/delete/126853/non-english Del: in docs -- https://main.php.net/note/delete/126853/in+docs Del: other reasons-- https://main.php.net/note/delete/126853 Reject -- https://main.php.net/note/reject/126853 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217617) next »