PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/helpers/Encoding.php

https://bitbucket.org/dereckson/source-templates-generator
PHP | 325 lines | 224 code | 37 blank | 64 comment | 35 complexity | 5e6fe9966603040c45510be5cb5d5440 MD5 | raw file
Possible License(s): GPL-3.0, BSD-2-Clause
  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@framework2.com.ar>
  30. * @package Encoding
  31. * @version 1.2
  32. * @link https://github.com/neitanod/forceutf8
  33. * @example https://github.com/neitanod/forceutf8
  34. * @license Revised BSD
  35. */
  36. //namespace ForceUTF8;
  37. class Encoding {
  38. protected static $win1252ToUtf8 = array(
  39. 128 => "\xe2\x82\xac",
  40. 130 => "\xe2\x80\x9a",
  41. 131 => "\xc6\x92",
  42. 132 => "\xe2\x80\x9e",
  43. 133 => "\xe2\x80\xa6",
  44. 134 => "\xe2\x80\xa0",
  45. 135 => "\xe2\x80\xa1",
  46. 136 => "\xcb\x86",
  47. 137 => "\xe2\x80\xb0",
  48. 138 => "\xc5\xa0",
  49. 139 => "\xe2\x80\xb9",
  50. 140 => "\xc5\x92",
  51. 142 => "\xc5\xbd",
  52. 145 => "\xe2\x80\x98",
  53. 146 => "\xe2\x80\x99",
  54. 147 => "\xe2\x80\x9c",
  55. 148 => "\xe2\x80\x9d",
  56. 149 => "\xe2\x80\xa2",
  57. 150 => "\xe2\x80\x93",
  58. 151 => "\xe2\x80\x94",
  59. 152 => "\xcb\x9c",
  60. 153 => "\xe2\x84\xa2",
  61. 154 => "\xc5\xa1",
  62. 155 => "\xe2\x80\xba",
  63. 156 => "\xc5\x93",
  64. 158 => "\xc5\xbe",
  65. 159 => "\xc5\xb8"
  66. );
  67. protected static $brokenUtf8ToUtf8 = array(
  68. "\xc2\x80" => "\xe2\x82\xac",
  69. "\xc2\x82" => "\xe2\x80\x9a",
  70. "\xc2\x83" => "\xc6\x92",
  71. "\xc2\x84" => "\xe2\x80\x9e",
  72. "\xc2\x85" => "\xe2\x80\xa6",
  73. "\xc2\x86" => "\xe2\x80\xa0",
  74. "\xc2\x87" => "\xe2\x80\xa1",
  75. "\xc2\x88" => "\xcb\x86",
  76. "\xc2\x89" => "\xe2\x80\xb0",
  77. "\xc2\x8a" => "\xc5\xa0",
  78. "\xc2\x8b" => "\xe2\x80\xb9",
  79. "\xc2\x8c" => "\xc5\x92",
  80. "\xc2\x8e" => "\xc5\xbd",
  81. "\xc2\x91" => "\xe2\x80\x98",
  82. "\xc2\x92" => "\xe2\x80\x99",
  83. "\xc2\x93" => "\xe2\x80\x9c",
  84. "\xc2\x94" => "\xe2\x80\x9d",
  85. "\xc2\x95" => "\xe2\x80\xa2",
  86. "\xc2\x96" => "\xe2\x80\x93",
  87. "\xc2\x97" => "\xe2\x80\x94",
  88. "\xc2\x98" => "\xcb\x9c",
  89. "\xc2\x99" => "\xe2\x84\xa2",
  90. "\xc2\x9a" => "\xc5\xa1",
  91. "\xc2\x9b" => "\xe2\x80\xba",
  92. "\xc2\x9c" => "\xc5\x93",
  93. "\xc2\x9e" => "\xc5\xbe",
  94. "\xc2\x9f" => "\xc5\xb8"
  95. );
  96. protected static $utf8ToWin1252 = array(
  97. "\xe2\x82\xac" => "\x80",
  98. "\xe2\x80\x9a" => "\x82",
  99. "\xc6\x92" => "\x83",
  100. "\xe2\x80\x9e" => "\x84",
  101. "\xe2\x80\xa6" => "\x85",
  102. "\xe2\x80\xa0" => "\x86",
  103. "\xe2\x80\xa1" => "\x87",
  104. "\xcb\x86" => "\x88",
  105. "\xe2\x80\xb0" => "\x89",
  106. "\xc5\xa0" => "\x8a",
  107. "\xe2\x80\xb9" => "\x8b",
  108. "\xc5\x92" => "\x8c",
  109. "\xc5\xbd" => "\x8e",
  110. "\xe2\x80\x98" => "\x91",
  111. "\xe2\x80\x99" => "\x92",
  112. "\xe2\x80\x9c" => "\x93",
  113. "\xe2\x80\x9d" => "\x94",
  114. "\xe2\x80\xa2" => "\x95",
  115. "\xe2\x80\x93" => "\x96",
  116. "\xe2\x80\x94" => "\x97",
  117. "\xcb\x9c" => "\x98",
  118. "\xe2\x84\xa2" => "\x99",
  119. "\xc5\xa1" => "\x9a",
  120. "\xe2\x80\xba" => "\x9b",
  121. "\xc5\x93" => "\x9c",
  122. "\xc5\xbe" => "\x9e",
  123. "\xc5\xb8" => "\x9f"
  124. );
  125. static function toUTF8($text){
  126. /**
  127. * Function Encoding::toUTF8
  128. *
  129. * This function leaves UTF8 characters alone, while converting almost all non-UTF8 to UTF8.
  130. *
  131. * It assumes that the encoding of the original string is either Windows-1252 or ISO 8859-1.
  132. *
  133. * It may fail to convert characters to UTF-8 if they fall into one of these scenarios:
  134. *
  135. * 1) when any of these characters: ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß
  136. * are followed by any of these: ("group B")
  137. * ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶•¸¹º»¼½¾¿
  138. * For example: %ABREPRESENT%C9%BB. «REPRESENTÉ»
  139. * The "«" (%AB) character will be converted, but the "É" followed by "»" (%C9%BB)
  140. * is also a valid unicode character, and will be left unchanged.
  141. *
  142. * 2) when any of these: àáâãäåæçèéêëìíîï are followed by TWO chars from group B,
  143. * 3) when any of these: ðñòó are followed by THREE chars from group B.
  144. *
  145. * @name toUTF8
  146. * @param string $text Any string.
  147. * @return string The same string, UTF8 encoded
  148. *
  149. */
  150. if(is_array($text))
  151. {
  152. foreach($text as $k => $v)
  153. {
  154. $text[$k] = self::toUTF8($v);
  155. }
  156. return $text;
  157. } elseif(is_string($text)) {
  158. $max = strlen($text);
  159. $buf = "";
  160. for($i = 0; $i < $max; $i++){
  161. $c1 = $text{$i};
  162. if($c1>="\xc0"){ //Should be converted to UTF8, if it's not UTF8 already
  163. $c2 = $i+1 >= $max? "\x00" : $text{$i+1};
  164. $c3 = $i+2 >= $max? "\x00" : $text{$i+2};
  165. $c4 = $i+3 >= $max? "\x00" : $text{$i+3};
  166. if($c1 >= "\xc0" & $c1 <= "\xdf"){ //looks like 2 bytes UTF8
  167. if($c2 >= "\x80" && $c2 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  168. $buf .= $c1 . $c2;
  169. $i++;
  170. } else { //not valid UTF8. Convert it.
  171. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  172. $cc2 = ($c1 & "\x3f") | "\x80";
  173. $buf .= $cc1 . $cc2;
  174. }
  175. } elseif($c1 >= "\xe0" & $c1 <= "\xef"){ //looks like 3 bytes UTF8
  176. if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  177. $buf .= $c1 . $c2 . $c3;
  178. $i = $i + 2;
  179. } else { //not valid UTF8. Convert it.
  180. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  181. $cc2 = ($c1 & "\x3f") | "\x80";
  182. $buf .= $cc1 . $cc2;
  183. }
  184. } elseif($c1 >= "\xf0" & $c1 <= "\xf7"){ //looks like 4 bytes UTF8
  185. if($c2 >= "\x80" && $c2 <= "\xbf" && $c3 >= "\x80" && $c3 <= "\xbf" && $c4 >= "\x80" && $c4 <= "\xbf"){ //yeah, almost sure it's UTF8 already
  186. $buf .= $c1 . $c2 . $c3;
  187. $i = $i + 2;
  188. } else { //not valid UTF8. Convert it.
  189. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  190. $cc2 = ($c1 & "\x3f") | "\x80";
  191. $buf .= $cc1 . $cc2;
  192. }
  193. } else { //doesn't look like UTF8, but should be converted
  194. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  195. $cc2 = (($c1 & "\x3f") | "\x80");
  196. $buf .= $cc1 . $cc2;
  197. }
  198. } elseif(($c1 & "\xc0") == "\x80"){ // needs conversion
  199. if(isset(self::$win1252ToUtf8[ord($c1)])) { //found in Windows-1252 special cases
  200. $buf .= self::$win1252ToUtf8[ord($c1)];
  201. } else {
  202. $cc1 = (chr(ord($c1) / 64) | "\xc0");
  203. $cc2 = (($c1 & "\x3f") | "\x80");
  204. $buf .= $cc1 . $cc2;
  205. }
  206. } else { // it doesn't need convesion
  207. $buf .= $c1;
  208. }
  209. }
  210. return $buf;
  211. } else {
  212. return $text;
  213. }
  214. }
  215. static function toWin1252($text) {
  216. if(is_array($text)) {
  217. foreach($text as $k => $v) {
  218. $text[$k] = self::toWin1252($v);
  219. }
  220. return $text;
  221. } elseif(is_string($text)) {
  222. return utf8_decode(str_replace(array_keys(self::$utf8ToWin1252), array_values(self::$utf8ToWin1252), self::toUTF8($text)));
  223. } else {
  224. return $text;
  225. }
  226. }
  227. static function toISO8859($text) {
  228. return self::toWin1252($text);
  229. }
  230. static function toLatin1($text) {
  231. return self::toWin1252($text);
  232. }
  233. static function fixUTF8($text){
  234. if(is_array($text)) {
  235. foreach($text as $k => $v) {
  236. $text[$k] = self::fixUTF8($v);
  237. }
  238. return $text;
  239. }
  240. $last = "";
  241. while($last <> $text){
  242. $last = $text;
  243. $text = self::toUTF8(utf8_decode(str_replace(array_keys(self::$utf8ToWin1252), array_values(self::$utf8ToWin1252), $text)));
  244. }
  245. $text = self::toUTF8(utf8_decode(str_replace(array_keys(self::$utf8ToWin1252), array_values(self::$utf8ToWin1252), $text)));
  246. return $text;
  247. }
  248. static function UTF8FixWin1252Chars($text){
  249. // If you received an UTF-8 string that was converted from Windows-1252 as it was ISO8859-1
  250. // (ignoring Windows-1252 chars from 80 to 9F) use this function to fix it.
  251. // See: http://en.wikipedia.org/wiki/Windows-1252
  252. return str_replace(array_keys(self::$brokenUtf8ToUtf8), array_values(self::$brokenUtf8ToUtf8), $text);
  253. }
  254. static function removeBOM($str=""){
  255. if(substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
  256. $str=substr($str, 3);
  257. }
  258. return $str;
  259. }
  260. public static function normalizeEncoding($encodingLabel)
  261. {
  262. $encoding = strtoupper($encodingLabel);
  263. $enc = preg_replace('/[^a-zA-Z0-9\s]/', '', $encoding);
  264. $equivalences = array(
  265. 'ISO88591' => 'ISO-8859-1',
  266. 'ISO8859' => 'ISO-8859-1',
  267. 'ISO' => 'ISO-8859-1',
  268. 'LATIN1' => 'ISO-8859-1',
  269. 'LATIN' => 'ISO-8859-1',
  270. 'UTF8' => 'UTF-8',
  271. 'UTF' => 'UTF-8',
  272. 'WIN1252' => 'ISO-8859-1',
  273. 'WINDOWS1252' => 'ISO-8859-1'
  274. );
  275. if(empty($equivalences[$encoding])){
  276. return 'UTF-8';
  277. }
  278. return $equivalences[$encoding];
  279. }
  280. public static function encode($encodingLabel, $text)
  281. {
  282. $encodingLabel = self::normalizeEncoding($encodingLabel);
  283. if($encodingLabel == 'UTF-8') return Encoding::toUTF8($text);
  284. if($encodingLabel == 'ISO-8859-1') return Encoding::toLatin1($text);
  285. }
  286. }