Encoding.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /*
  3. Copyright (c) 2008 Sebastián Grignoli
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. Neither the name of copyright holders nor the names of its
  14. contributors may be used to endorse or promote products derived
  15. from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  18. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
  20. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @author "Sebastián Grignoli" <grignoli@gmail.com>
  30. * @package Encoding
  31. * @version 2.0
  32. * @link https://github.com/neitanod/forceutf8
  33. * @example https://github.com/neitanod/forceutf8
  34. * @license Revised BSD
  35. */
  36. namespace External\ForceUTF8;
  37. class Encoding {
  38. const ICONV_TRANSLIT = "TRANSLIT";
  39. const ICONV_IGNORE = "IGNORE";
  40. const WITHOUT_ICONV = "";
  41. protected static $win1252ToUtf8 = array(
  42. 128 => "\xe2\x82\xac",
  43. 130 => "\xe2\x80\x9a",
  44. 131 => "\xc6\x92",
  45. 132 => "\xe2\x80\x9e",
  46. 133 => "\xe2\x80\xa6",
  47. 134 => "\xe2\x80\xa0",
  48. 135 => "\xe2\x80\xa1",
  49. 136 => "\xcb\x86",
  50. 137 => "\xe2\x80\xb0",
  51. 138 => "\xc5\xa0",
  52. 139 => "\xe2\x80\xb9",
  53. 140 => "\xc5\x92",
  54. 142 => "\xc5\xbd",
  55. 145 => "\xe2\x80\x98",
  56. 146 => "\xe2\x80\x99",
  57. 147 => "\xe2\x80\x9c",
  58. 148 => "\xe2\x80\x9d",
  59. 149 => "\xe2\x80\xa2",
  60. 150 => "\xe2\x80\x93",
  61. 151 => "\xe2\x80\x94",
  62. 152 => "\xcb\x9c",
  63. 153 => "\xe2\x84\xa2",
  64. 154 => "\xc5\xa1",
  65. 155 => "\xe2\x80\xba",
  66. 156 => "\xc5\x93",
  67. 158 => "\xc5\xbe",
  68. 159 => "\xc5\xb8"
  69. );
  70. protected static $brokenUtf8ToUtf8 = array(
  71. "\xc2\x80" => "\xe2\x82\xac",
  72. "\xc2\x82" => "\xe2\x80\x9a",
  73. "\xc2\x83" => "\xc6\x92",
  74. "\xc2\x84" => "\xe2\x80\x9e",
  75. "\xc2\x85" => "\xe2\x80\xa6",
  76. "\xc2\x86" => "\xe2\x80\xa0",
  77. "\xc2\x87" => "\xe2\x80\xa1",
  78. "\xc2\x88" => "\xcb\x86",
  79. "\xc2\x89" => "\xe2\x80\xb0",
  80. "\xc2\x8a" => "\xc5\xa0",
  81. "\xc2\x8b" => "\xe2\x80\xb9",
  82. "\xc2\x8c" => "\xc5\x92",
  83. "\xc2\x8e" => "\xc5\xbd",
  84. "\xc2\x91" => "\xe2\x80\x98",
  85. "\xc2\x92" => "\xe2\x80\x99",
  86. "\xc2\x93" => "\xe2\x80\x9c",
  87. "\xc2\x94" => "\xe2\x80\x9d",
  88. "\xc2\x95" => "\xe2\x80\xa2",
  89. "\xc2\x96" => "\xe2\x80\x93",
  90. "\xc2\x97" => "\xe2\x80\x94",
  91. "\xc2\x98" => "\xcb\x9c",
  92. "\xc2\x99" => "\xe2\x84\xa2",
  93. "\xc2\x9a" => "\xc5\xa1",
  94. "\xc2\x9b" => "\xe2\x80\xba",
  95. "\xc2\x9c" => "\xc5\x93",
  96. "\xc2\x9e" => "\xc5\xbe",
  97. "\xc2\x9f" => "\xc5\xb8"
  98. );
  99. protected static $utf8ToWin1252 = array(
  100. "\xe2\x82\xac" => "\x80",
  101. "\xe2\x80\x9a" => "\x82",
  102. "\xc6\x92" => "\x83",
  103. "\xe2\x80\x9e" => "\x84",
  104. "\xe2\x80\xa6" => "\x85",
  105. "\xe2\x80\xa0" => "\x86",
  106. "\xe2\x80\xa1" => "\x87",
  107. "\xcb\x86" => "\x88",
  108. "\xe2\x80\xb0" => "\x89",
  109. "\xc5\xa0" => "\x8a",
  110. "\xe2\x80\xb9" => "\x8b",
  111. "\xc5\x92" => "\x8c",
  112. "\xc5\xbd" => "\x8e",
  113. "\xe2\x80\x98" => "\x91",
  114. "\xe2\x80\x99" => "\x92",
  115. "\xe2\x80\x9c" => "\x93",
  116. "\xe2\x80\x9d" => "\x94",
  117. "\xe2\x80\xa2" => "\x95",
  118. "\xe2\x80\x93" => "\x96",
  119. "\xe2\x80\x94" => "\x97",
  120. "\xcb\x9c" => "\x98",
  121. "\xe2\x84\xa2" => "\x99",
  122. "\xc5\xa1" => "\x9a",
  123. "\xe2\x80\xba" => "\x9b",
  124. "\xc5\x93" => "\x9c",
  125. "\xc5\xbe" => "\x9e",
  126. "\xc5\xb8" => "\x9f"
  127. );
  128. static function toUTF8($text){
  129. /**
  130. * Function \ForceUTF8\Encoding::toUTF8
  131. *
  132. * This function leaves UTF8 characters alone, while converting almost all non-UTF8 to UTF8.
  133. *
  134. * It assumes that the encoding of the original string is either Windows-1252 or ISO 8859-1.
  135. *
  136. * It may fail to convert characters to UTF-8 if they fall into one of these scenarios:
  137. *
  138. * 1) when any of these characters: ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß
  139. * are followed by any of these: ("group B")
  140. * ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶•¸¹º»¼½¾¿
  141. * For example: %ABREPRESENT%C9%BB. «REPRESENTÉ»
  142. * The "«" (%AB) character will be converted, but the "É" followed by "»" (%C9%BB)
  143. * is also a valid unicode character, and will be left unchanged.
  144. *
  145. * 2) when any of these: àáâãäåæçèéêëìíîï are followed by TWO chars from group B,
  146. * 3) when any of these: ðñòó are followed by THREE chars from group B.
  147. *
  148. * @name toUTF8
  149. * @param string $text Any string.
  150. * @return string The same string, UTF8 encoded
  151. *
  152. */
  153. if(is_array($text))
  154. {
  155. foreach($text as $k => $v)
  156. {
  157. $text[$k] = self::toUTF8($v);
  158. }
  159. return $text;
  160. }
  161. if(!is_string($text)) {
  162. return $text;
  163. }
  164. $max = self::strlen($text);
  165. $buf = "";
  166. for($i = 0; $i < $max; $i++){
  167. $c1 = $text{$i};
  168. if($c1>="\xc0"){ //Should be converted to UTF8, if it's not UTF8 already
  169. $c2 = $i+1 >= $max? "\x00" : $text{$i+1};
  170. $c3 = $i+2 >= $max? "\x00" : $text{$i+2};
  171. $c4 = $i+3 >= $max? "\x00" : $text{$i+3};
  172. if($c1 >= "\xc0" & $c1 <= "\xdf"){ //looks like 2 bytes UTF8
  173. if($c2 >= "\x80" && $c2 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  174. $buf .= $c1 . $c2;
  175. $i++;
  176. } else { //not valid UTF8. Convert it.
  177. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  178. $cc2 = ($c1 & "\x3f") | "\x80";
  179. $buf .= $cc1 . $cc2;
  180. }
  181. } elseif($c1 >= "\xe0" & $c1 <= "\xef"){ //looks like 3 bytes UTF8
  182. if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  183. $buf .= $c1 . $c2 . $c3;
  184. $i = $i + 2;
  185. } else { //not valid UTF8. Convert it.
  186. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  187. $cc2 = ($c1 & "\x3f") | "\x80";
  188. $buf .= $cc1 . $cc2;
  189. }
  190. } elseif($c1 >= "\xf0" & $c1 <= "\xf7"){ //looks like 4 bytes UTF8
  191. if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf" && $c4 >= "\x80" && $c4 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  192. $buf .= $c1 . $c2 . $c3 . $c4;
  193. $i = $i + 3;
  194. } else { //not valid UTF8. Convert it.
  195. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  196. $cc2 = ($c1 & "\x3f") | "\x80";
  197. $buf .= $cc1 . $cc2;
  198. }
  199. } else { //doesn't look like UTF8, but should be converted
  200. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  201. $cc2 = (($c1 & "\x3f") | "\x80");
  202. $buf .= $cc1 . $cc2;
  203. }
  204. } elseif(($c1 & "\xc0") == "\x80"){ // needs conversion
  205. if(isset(self::$win1252ToUtf8[ord($c1)])) { //found in Windows-1252 special cases
  206. $buf .= self::$win1252ToUtf8[ord($c1)];
  207. } else {
  208. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  209. $cc2 = (($c1 & "\x3f") | "\x80");
  210. $buf .= $cc1 . $cc2;
  211. }
  212. } else { // it doesn't need conversion
  213. $buf .= $c1;
  214. }
  215. }
  216. return $buf;
  217. }
  218. static function toWin1252($text, $option = self::WITHOUT_ICONV) {
  219. if(is_array($text)) {
  220. foreach($text as $k => $v) {
  221. $text[$k] = self::toWin1252($v, $option);
  222. }
  223. return $text;
  224. } elseif(is_string($text)) {
  225. return static::utf8_decode($text, $option);
  226. } else {
  227. return $text;
  228. }
  229. }
  230. static function toISO8859($text) {
  231. return self::toWin1252($text);
  232. }
  233. static function toLatin1($text) {
  234. return self::toWin1252($text);
  235. }
  236. static function fixUTF8($text, $option = self::WITHOUT_ICONV){
  237. if(is_array($text)) {
  238. foreach($text as $k => $v) {
  239. $text[$k] = self::fixUTF8($v, $option);
  240. }
  241. return $text;
  242. }
  243. $last = "";
  244. while($last <> $text){
  245. $last = $text;
  246. $text = self::toUTF8(static::utf8_decode($text, $option));
  247. }
  248. $text = self::toUTF8(static::utf8_decode($text, $option));
  249. return $text;
  250. }
  251. static function UTF8FixWin1252Chars($text){
  252. // If you received an UTF-8 string that was converted from Windows-1252 as it was ISO8859-1
  253. // (ignoring Windows-1252 chars from 80 to 9F) use this function to fix it.
  254. // See: http://en.wikipedia.org/wiki/Windows-1252
  255. return str_replace(array_keys(self::$brokenUtf8ToUtf8), array_values(self::$brokenUtf8ToUtf8), $text);
  256. }
  257. static function removeBOM($str=""){
  258. if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
  259. $str=substr($str, 3);
  260. }
  261. return $str;
  262. }
  263. protected static function strlen($text){
  264. return (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload')) & 2) ?
  265. mb_strlen($text,'8bit') : strlen($text);
  266. }
  267. public static function normalizeEncoding($encodingLabel)
  268. {
  269. $encoding = strtoupper($encodingLabel);
  270. $encoding = preg_replace('/[^a-zA-Z0-9\s]/', '', $encoding);
  271. $equivalences = array(
  272. 'ISO88591' => 'ISO-8859-1',
  273. 'ISO8859' => 'ISO-8859-1',
  274. 'ISO' => 'ISO-8859-1',
  275. 'LATIN1' => 'ISO-8859-1',
  276. 'LATIN' => 'ISO-8859-1',
  277. 'UTF8' => 'UTF-8',
  278. 'UTF' => 'UTF-8',
  279. 'WIN1252' => 'ISO-8859-1',
  280. 'WINDOWS1252' => 'ISO-8859-1'
  281. );
  282. if(empty($equivalences[$encoding])){
  283. return 'UTF-8';
  284. }
  285. return $equivalences[$encoding];
  286. }
  287. public static function encode($encodingLabel, $text)
  288. {
  289. $encodingLabel = self::normalizeEncoding($encodingLabel);
  290. if($encodingLabel == 'ISO-8859-1') return self::toLatin1($text);
  291. return self::toUTF8($text);
  292. }
  293. protected static function utf8_decode($text, $option)
  294. {
  295. if ($option == self::WITHOUT_ICONV || !function_exists('iconv')) {
  296. $o = utf8_decode(
  297. str_replace(array_keys(self::$utf8ToWin1252), array_values(self::$utf8ToWin1252), self::toUTF8($text))
  298. );
  299. } else {
  300. $o = iconv("UTF-8", "Windows-1252" . ($option == self::ICONV_TRANSLIT ? '//TRANSLIT' : ($option == self::ICONV_IGNORE ? '//IGNORE' : '')), $text);
  301. }
  302. return $o;
  303. }
  304. }