home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / utf8.inc < prev    next >
Encoding:
Text File  |  2003-03-16  |  2.8 KB  |  102 lines

  1. <?php
  2. /////////////////////////////
  3. //    utf8.inc
  4. //    (C)2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
  5. //        
  6. //        Description:
  7. //                UTF-8 handling functions
  8. //
  9. //    This file is part of IlohaMail. IlohaMail is free software released 
  10. //    under the GPL license.  See enclosed file COPYING for details, or 
  11. //    see http://www.fsf.org/copyleft/gpl.html
  12. ////////////////////////////
  13.  
  14. /**
  15. * takes a string of utf-8 encoded characters and converts it to a string of unicode entities
  16. * each unicode entitiy has the form &#nnnnn; n={0..9} and can be displayed by utf-8 supporting
  17. * browsers
  18. * @param $source string encoded using utf-8 [STRING]
  19. * @return string of unicode entities [STRING]
  20. * @access public
  21. */
  22. /**
  23. * Author: ronen at greyzone dot com
  24. * Taken from php.net comment:
  25. *    http://www.php.net/manual/en/function.utf8-decode.php
  26. **/
  27. function utf8ToUnicodeEntities ($source) {
  28.     // array used to figure what number to decrement from character order value
  29.     // according to number of characters used to map unicode to ascii by utf-8
  30.     $decrement[4] = 240;
  31.     $decrement[3] = 224;
  32.     $decrement[2] = 192;
  33.     $decrement[1] = 0;
  34.  
  35.     // the number of bits to shift each charNum by
  36.     $shift[1][0] = 0;
  37.     $shift[2][0] = 6;
  38.     $shift[2][1] = 0;
  39.     $shift[3][0] = 12;
  40.     $shift[3][1] = 6;
  41.     $shift[3][2] = 0;
  42.     $shift[4][0] = 18;
  43.     $shift[4][1] = 12;
  44.     $shift[4][2] = 6;
  45.     $shift[4][3] = 0;
  46.  
  47.     $pos = 0;
  48.     $len = strlen ($source);
  49.     $encodedString = '';
  50.     while ($pos < $len) {
  51.         $asciiPos = ord (substr ($source, $pos, 1));
  52.         if (($asciiPos >= 240) && ($asciiPos <= 255)) {
  53.             // 4 chars representing one unicode character
  54.             $thisLetter = substr ($source, $pos, 4);
  55.             $pos += 4;
  56.         }
  57.         else if (($asciiPos >= 224) && ($asciiPos <= 239)) {
  58.             // 3 chars representing one unicode character
  59.             $thisLetter = substr ($source, $pos, 3);
  60.             $pos += 3;
  61.         }
  62.         else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
  63.             // 2 chars representing one unicode character
  64.             $thisLetter = substr ($source, $pos, 2);
  65.             $pos += 2;
  66.         }
  67.         else {
  68.             // 1 char (lower ascii)
  69.             $thisLetter = substr ($source, $pos, 1);
  70.             $pos += 1;
  71.         }
  72.  
  73.         // process the string representing the letter to a unicode entity
  74.         $thisLen = strlen ($thisLetter);
  75.         $thisPos = 0;
  76.         $decimalCode = 0;
  77.         while ($thisPos < $thisLen) {
  78.             $thisCharOrd = ord (substr ($thisLetter, $thisPos, 1));
  79.             if ($thisPos == 0) {
  80.                 $charNum = intval ($thisCharOrd - $decrement[$thisLen]);
  81.                 $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
  82.             }
  83.             else {
  84.                 $charNum = intval ($thisCharOrd - 128);
  85.                 $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
  86.             }
  87.  
  88.             $thisPos++;
  89.         }
  90.  
  91.         if ($thisLen == 1)
  92.             $encodedLetter = "&#". str_pad($decimalCode, 3, "0", STR_PAD_LEFT) . ';';
  93.         else
  94.             $encodedLetter = "&#". str_pad($decimalCode, 5, "0", STR_PAD_LEFT) . ';';
  95.  
  96.         $encodedString .= $encodedLetter;
  97.     }
  98.  
  99.     return $encodedString;
  100. }
  101.  
  102. ?>