source-class-Com.Tecnick.Unicode.Convert.Encoding

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Encoding.php
  4:  *
  5:  * @since       2011-05-23
  6:  * @category    Library
  7:  * @package     Unicode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-unicode
 12:  *
 13:  * This file is part of tc-lib-unicode software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Unicode\Convert;
 17: 
 18: use \Com\Tecnick\Unicode\Data\Latin as Latin;
 19: 
 20: /**
 21:  * Com\Tecnick\Unicode\Convert\Encoding
 22:  *
 23:  * @since       2015-07-13
 24:  * @category    Library
 25:  * @package     Unicode
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2011-2015 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-unicode
 30:  */
 31: class Encoding
 32: {
 33:     /**
 34:      * Converts UTF-8 code array to Latin1 codes
 35:      *
 36:      * @param array $ordarr Array containing UTF-8 code points
 37:      *
 38:      * @return array
 39:      */
 40:     public function uniArrToLatinArr(array $ordarr)
 41:     {
 42:         $latarr = array();
 43:         foreach ($ordarr as $chr) {
 44:             if ($chr < 256) {
 45:                 $latarr[] = $chr;
 46:             } elseif (array_key_exists($chr, Latin::$substitute)) {
 47:                 $latarr[] = Latin::$substitute[$chr];
 48:             } elseif ($chr !== 0xFFFD) {
 49:                 $latarr[] = 63; // '?' character
 50:             }
 51:         }
 52:         return $latarr;
 53:     }
 54: 
 55:     /**
 56:      * Converts an array of Latin1 code points to a string
 57:      *
 58:      * @param array $latarr Array of Latin1 code points
 59:      *
 60:      * @return string
 61:      */
 62:     public function latinArrToStr(array $latarr)
 63:     {
 64:         return implode(array_map('chr', $latarr));
 65:     }
 66: 
 67:     /**
 68:      * Convert a string to an hexadecimal string (byte string) representation (as in the PDF standard)
 69:      *
 70:      * @param string $str  String to convert
 71:      *
 72:      * @return string
 73:      */
 74:     public function strToHex($str)
 75:     {
 76:         $hexstr = '';
 77:         $len = strlen($str);
 78:         for ($idx = 0; $idx < $len; ++$idx) {
 79:             $hexstr .= sprintf('%02s', dechex(ord($str[$idx])));
 80:         }
 81:         return $hexstr;
 82:     }
 83: 
 84:     /**
 85:      * Convert an hexadecimal string (byte string - as in the PDF standard) to string
 86:      *
 87:      * @param $bs (string) byte string to convert
 88:      *
 89:      * @return string
 90:      */
 91:     public function hexToStr($hex)
 92:     {
 93:         if (strlen($hex) == 0) {
 94:             return '';
 95:         }
 96:         $str = '';
 97:         $bytes = str_split($hex, 2);
 98:         foreach ($bytes as $chr) {
 99:             $str .= chr(hexdec($chr));
100:         }
101:         return $str;
102:     }
103: 
104:     /**
105:      * Converts a string with an unknown encoding to UTF-8
106:      *
107:      * @param string $str String to convert
108:      * @param mixed  $enc Array or comma separated list string of encodings
109:      *
110:      * @return string UTF-8 encoded string
111:      */
112:     public function toUTF8($str, $enc = null)
113:     {
114:         if ($enc === null) {
115:             $enc = mb_detect_order();
116:         }
117:         return mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str, $enc));
118:     }
119: 
120:     /**
121:      * Converts an UTF-8 string to UTF-16BE
122:      *
123:      * @param string $str UTF-8 String to convert
124:      *
125:      * @return string UTF-16BE encoded string
126:      */
127:     public function toUTF16BE($str)
128:     {
129:         return mb_convert_encoding($str, 'UTF-16BE', 'UTF-8');
130:     }
131: }
132: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy