[ Index ]

PHP Cross Reference of phpwcms V1.4.3 _r380 (23.11.09)

title

Body

[close]

/include/inc_ext/ConvertCharset/ -> ConvertCharset.class.php (source)

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


Generated: Wed Dec 30 05:55:15 2009 Cross-referenced by PHPXref 0.7