[ Index ] |
PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10) |
[Summary view] [Print] [Text view]
1 <?php 2 3 define ("CONVERT_TABLES_DIR", PHPWCMS_ROOT.'/include/inc_ext/ConvertCharset/ConvertTables/'); 4 define ("DEBUG_MODE", 1); 5 6 7 class ConvertCharset{ 8 var $RecognizedEncoding; // (boolean) This value keeps information if string contains multibyte chars. 9 var $Entities; // (boolean) This value keeps information if output should be with numeric entities. 10 var $FromCharset; // (string) This value keeps information about source (from) encoding 11 var $ToCharset; // (string) This value keeps information about destination (to) encoding 12 var $CharsetTable; // (array) This property keeps convert Table inside 13 14 15 16 function ConvertCharset ($FromCharset, $ToCharset, $TurnOnEntities = false) 17 { 18 19 $this -> FromCharset = strtolower($FromCharset); 20 $this -> ToCharset = strtolower($ToCharset); 21 $this -> Entities = $TurnOnEntities; 22 23 24 if ($this -> FromCharset == $this -> ToCharset) 25 { 26 print $this -> DebugOutput(1, 0, $this -> FromCharset); 27 } 28 if (($this -> FromCharset == $this -> ToCharset) AND ($this -> FromCharset == "utf-8")) 29 { 30 print $this -> DebugOutput(0, 4, $this -> FromCharset); 31 exit; 32 } 33 34 35 if ($this -> FromCharset == "utf-8") 36 { 37 $this -> CharsetTable = $this -> MakeConvertTable ($this -> ToCharset); 38 } 39 else if ($this -> ToCharset == "utf-8") 40 { 41 $this -> CharsetTable = $this -> MakeConvertTable ($this -> FromCharset); 42 } 43 else 44 { 45 $this -> CharsetTable = $this -> MakeConvertTable ($this -> FromCharset, $this -> ToCharset); 46 } 47 48 } 49 50 51 function UnicodeEntity ($UnicodeString) 52 { 53 $OutString = ""; 54 $StringLenght = strlen ($UnicodeString); 55 for ($CharPosition = 0; $CharPosition < $StringLenght; $CharPosition++) 56 { 57 $Char = $UnicodeString [$CharPosition]; 58 $AsciiChar = ord ($Char); 59 60 if ($AsciiChar < 128){ 61 $OutString .= $Char; 62 } 63 else if ($AsciiChar >> 5 == 6){ 64 $FirstByte = ($AsciiChar & 31); 65 $CharPosition++; 66 $Char = $UnicodeString [$CharPosition]; 67 $AsciiChar = ord ($Char); 68 $SecondByte = ($AsciiChar & 63); 69 $AsciiChar = ($FirstByte * 64) + $SecondByte; 70 $Entity = sprintf ("&#%d;", $AsciiChar); 71 $OutString .= $Entity; 72 } 73 else if ($AsciiChar >> 4 == 14){ 74 $FirstByte = ($AsciiChar & 31); 75 $CharPosition++; 76 $Char = $UnicodeString [$CharPosition]; 77 $AsciiChar = ord ($Char); 78 $SecondByte = ($AsciiChar & 63); 79 $CharPosition++; 80 $Char = $UnicodeString [$CharPosition]; 81 $AsciiChar = ord ($Char); 82 $ThidrByte = ($AsciiChar & 63); 83 $AsciiChar = ((($FirstByte * 64) + $SecondByte) * 64) + $ThidrByte; 84 85 $Entity = sprintf ("&#%d;", $AsciiChar); 86 $OutString .= $Entity; 87 } 88 else if ($AsciiChar >> 3 == 30){ 89 $FirstByte = ($AsciiChar & 31); 90 $CharPosition++; 91 $Char = $UnicodeString [$CharPosition]; 92 $AsciiChar = ord ($Char); 93 $SecondByte = ($AsciiChar & 63); 94 $CharPosition++; 95 $Char = $UnicodeString [$CharPosition]; 96 $AsciiChar = ord ($Char); 97 $ThidrByte = ($AsciiChar & 63); 98 $CharPosition++; 99 $Char = $UnicodeString [$CharPosition]; 100 $AsciiChar = ord ($Char); 101 $FourthByte = ($AsciiChar & 63); 102 $AsciiChar = ((((($FirstByte * 64) + $SecondByte) * 64) + $ThidrByte) * 64) + $FourthByte; 103 104 $Entity = sprintf ("&#%d;", $AsciiChar); 105 $OutString .= $Entity; 106 } 107 } 108 return $OutString; 109 } 110 111 112 function HexToUtf ($UtfCharInHex) 113 { 114 $OutputChar = ""; 115 $UtfCharInDec = hexdec($UtfCharInHex); 116 if($UtfCharInDec < 128) $OutputChar .= chr($UtfCharInDec); 117 else if($UtfCharInDec < 2048)$OutputChar .= chr(($UtfCharInDec >> 6) + 192) . chr(($UtfCharInDec & 63) + 128); 118 else if($UtfCharInDec < 65536)$OutputChar .= chr(($UtfCharInDec >> 12) + 224) . chr((($UtfCharInDec >> 6) & 63) + 128) . chr(($UtfCharInDec & 63) + 128); 119 else if($UtfCharInDec < 2097152)$OutputChar .= chr($UtfCharInDec >> 18 + 240) . chr((($UtfCharInDec >> 12) & 63) + 128) . chr(($UtfCharInDec >> 6) & 63 + 128) . chr($UtfCharInDec & 63 + 128); 120 return $OutputChar; 121 } 122 123 124 125 function MakeConvertTable ($FromCharset, $ToCharset = '') 126 { 127 $ConvertTable = array(); 128 for($i = 0; $i < func_num_args(); $i++) 129 { 130 131 $FileName = func_get_arg($i); 132 if (!is_file(CONVERT_TABLES_DIR . $FileName)) 133 { 134 print $this -> DebugOutput(0, 0, CONVERT_TABLES_DIR . $FileName); //Print an error message 135 exit; 136 } 137 $FileWithEncTabe = fopen(CONVERT_TABLES_DIR . $FileName, "r") or die(); //This die(); is just to make sure... 138 while(!feof($FileWithEncTabe)) 139 { 140 141 if($OneLine = trim(fgets($FileWithEncTabe, 1024))) 142 { 143 144 if (substr($OneLine, 0, 1) != "#") 145 { 146 147 $HexValue = preg_split ("/[\s,]+/", $OneLine, 3); //We need only first 2 values 148 149 if (substr($HexValue[1], 0, 1) != "#") 150 { 151 $ArrayKey = strtoupper(str_replace(strtolower("0x"), "", $HexValue[1])); 152 $ArrayValue = strtoupper(str_replace(strtolower("0x"), "", $HexValue[0])); 153 $ConvertTable[func_get_arg($i)][$ArrayKey] = $ArrayValue; 154 } 155 } //if (substr($OneLine,... 156 } //if($OneLine=trim(f... 157 } //while(!feof($FirstFileWi... 158 } //for($i = 0; $i < func_... 159 160 if(!is_array($ConvertTable[$FromCharset])) $ConvertTable[$FromCharset] = array(); 161 162 if ((func_num_args() > 1) && (count($ConvertTable[$FromCharset]) == count($ConvertTable[$ToCharset])) && (count(array_diff_assoc($ConvertTable[$FromCharset], $ConvertTable[$ToCharset])) == 0)) 163 { 164 print $this -> DebugOutput(1, 1, "$FromCharset, $ToCharset"); 165 } 166 return $ConvertTable; 167 } 168 169 170 function Convert ($StringToChange) 171 { 172 if(!strlen($StringToChange)) return ''; 173 $StringToChange = (string)($StringToChange); 174 175 if($this -> FromCharset == $this -> ToCharset) return $StringToChange; 176 177 $NewString = ""; 178 179 180 if ($this -> FromCharset != "utf-8") 181 { 182 183 for ($i = 0; $i < strlen($StringToChange); $i++) 184 { 185 $HexChar = ""; 186 $UnicodeHexChar = ""; 187 $HexChar = strtoupper(dechex(ord($StringToChange[$i]))); 188 if (strlen($HexChar) == 1) $HexChar = "0" . $HexChar; 189 if (($this -> FromCharset == "gsm0338") && ($HexChar == '1B')){ 190 $i++; 191 $HexChar .= strtoupper(dechex(ord($StringToChange[$i]))); 192 } 193 if ($this -> ToCharset != "utf-8") 194 { 195 if (in_array($HexChar, $this -> CharsetTable[$this -> FromCharset])) 196 { 197 $UnicodeHexChar = array_search($HexChar, $this -> CharsetTable[$this -> FromCharset]); 198 $UnicodeHexChars = explode("+", $UnicodeHexChar); 199 for($UnicodeHexCharElement = 0; $UnicodeHexCharElement < count($UnicodeHexChars); $UnicodeHexCharElement++) 200 { 201 if (array_key_exists($UnicodeHexChars[$UnicodeHexCharElement], $this -> CharsetTable[$this -> ToCharset])) 202 { 203 if ($this -> Entities == true) 204 { 205 $NewString .= $this -> UnicodeEntity($this -> HexToUtf($UnicodeHexChars[$UnicodeHexCharElement])); 206 } 207 else 208 { 209 $NewString .= chr(hexdec($this -> CharsetTable[$this -> ToCharset][$UnicodeHexChars[$UnicodeHexCharElement]])); 210 } 211 } 212 else 213 { 214 print $this -> DebugOutput(0, 1, $StringToChange[$i]); 215 } 216 } //for($UnicodeH... 217 } 218 else 219 { 220 print $this -> DebugOutput(0, 2, $StringToChange[$i]); 221 } 222 } 223 else 224 { 225 if (in_array("$HexChar", $this -> CharsetTable[$this -> FromCharset])) 226 { 227 $UnicodeHexChar = array_search($HexChar, $this -> CharsetTable[$this -> FromCharset]); 228 229 $UnicodeHexChars = explode("+", $UnicodeHexChar); 230 for($UnicodeHexCharElement = 0; $UnicodeHexCharElement < count($UnicodeHexChars); $UnicodeHexCharElement++) 231 { 232 if ($this -> Entities == true) 233 { 234 $NewString .= $this -> UnicodeEntity($this -> HexToUtf($UnicodeHexChars[$UnicodeHexCharElement])); 235 } 236 else 237 { 238 $NewString .= $this -> HexToUtf($UnicodeHexChars[$UnicodeHexCharElement]); 239 } 240 } // for 241 } 242 else 243 { 244 print $this -> DebugOutput(0, 2, $StringToChange[$i]); 245 } 246 } 247 } 248 } 249 250 else if($this -> FromCharset == "utf-8") 251 { 252 $HexChar = ""; 253 $UnicodeHexChar = ""; 254 $this -> CharsetTable = $this -> MakeConvertTable ($this -> ToCharset); 255 foreach ($this -> CharsetTable[$this -> ToCharset] as $UnicodeHexChar => $HexChar) 256 { 257 if ($this -> Entities == true){ 258 $EntitieOrChar = $this -> UnicodeEntity($this -> HexToUtf($UnicodeHexChar)); 259 } 260 else 261 { 262 $EntitieOrChar = chr(hexdec($HexChar)); 263 } 264 $StringToChange = str_replace($this -> HexToUtf($UnicodeHexChar), $EntitieOrChar, $StringToChange); 265 } 266 $NewString = $StringToChange; 267 } 268 269 return $NewString; 270 } 271 272 273 function ConvertArray(& $array) 274 { 275 if (!is_array($array)) 276 { 277 $array = $this -> Convert($array); 278 return; 279 } 280 while(list($k, $v) = each($array)) 281 { 282 $this -> ConvertArray($v); 283 $array[$k] = $v; 284 } 285 } 286 287 288 function DebugOutput ($Group, $Number, $Value = false) 289 { 290 $Debug[0][0] = "Error, can NOT read file: " . $Value . "<br>"; 291 $Debug[0][1] = "Error, can't find maching char \"" . $Value . "\" in destination encoding table!" . "<br>"; 292 $Debug[0][2] = "Error, can't find maching char \"" . $Value . "\" in source encoding table!" . "<br>"; 293 $Debug[0][3] = "Error, you did NOT set variable " . $Value . " in Convert() function." . "<br>"; 294 $Debug[0][4] = "You can NOT convert string from " . $Value . " to " . $Value . "!" . "<BR>"; 295 $Debug[1][0] = "Notice, you are trying to convert string from " . $Value . " to " . $Value . ", don't you feel it's strange? ;-)" . "<br>"; 296 $Debug[1][1] = "Notice, both charsets " . $Value . " are identical! Check encoding tables files." . "<br>"; 297 $Debug[1][2] = "Notice, there is no unicode char in the string you are trying to convert." . "<br>"; 298 299 if (DEBUG_MODE >= $Group) 300 { 301 return $Debug[$Group][$Number]; 302 } 303 } // function DebugOutput 304 305 } //class ends here 306 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Nov 16 22:51:00 2010 | Cross-referenced by PHPXref 0.7 |