KSC5601
[ class tree: KSC5601 ] [ index: KSC5601 ] [ all elements ]

Source for file UTF8.php

Documentation is available at UTF8.php

  1. <?php
  2. /**
  3.  * KSC5601 UTF8 internal API for pure code
  4.  *
  5.  * @category   Charset
  6.  * @package    KSC5601
  7.  * @subpackage KSC5601_pure
  8.  * @author     JoungKyun.Kim <http://oops.org>
  9.  * @copyright  (c) 2015, JoungKyun.Kim
  10.  * @license    BSD License
  11.  * @version    $Id$
  12.  * @link       http://pear.oops.org/package/KSC5601
  13.  * @filesource
  14.  */
  15.  
  16. /**
  17.  * import High level API for convert character set
  18.  */
  19. require_once 'KSC5601/Stream.php';
  20.  
  21. /**
  22.  * Only PHP don't support iconv/mbstring, UCS2.php is needed
  23.  */
  24. if EXTMODE === false {
  25.     /**
  26.      * import API class that controls UCS2
  27.      */
  28.     require_once 'KSC5601/UCS2.php';
  29. else {
  30.     /**
  31.      * If needless USC2.php, define dummy class for compotable
  32.      * @package KSC5601
  33.      * @ignore
  34.      */
  35.     Class KSC5601_UCS2 {}
  36. }
  37.  
  38. /**
  39.  * UTF8 controle class api
  40.  *
  41.  * @package KSC5601
  42.  */
  43. class KSC5601_UTF8 extends KSC5601_UCS2
  44. {
  45.     // {{{ properties
  46.     /**
  47.      * Whether print debug message
  48.      *
  49.      * @access private
  50.      */
  51.     private $debug false;
  52.     // }}}
  53.  
  54.     // {{{ function rm_utf8bom ($s)
  55.     /**
  56.      * remove utf8 bom code (first 3byte)
  57.      *
  58.      * @access public
  59.      * @return string 
  60.      * @param  string Given strings
  61.      */
  62.     function rm_utf8bom ($s{
  63.         if ord ($s[0]== 0xef && ord ($s[1]== 0xbb && ord ($s[2]== 0xbf )
  64.             return substr ($s3);
  65.  
  66.         return $s;
  67.     }
  68.     // }}}
  69.  
  70.     // {{{ function is_utf8 ($s, $ascii)
  71.     /**
  72.      * whether utf8 or not given strings
  73.      *
  74.      * @access public
  75.      * @return boolean If given strings ars utf-8, return true
  76.      * @param  string  Given strings
  77.      * @param  boolean Check whether is ascii only or not
  78.      */
  79.     function is_utf8 ($s$ascii false{
  80.         if ord ($s[0]== 0xef && ord ($s[1]== 0xbb && ord ($s[2]== 0xbf )
  81.             return true;
  82.  
  83.         $ascii_status true;
  84.         $l strlen ($s);
  85.  
  86.         for $i=0$i<$l$i++ {
  87.             # if single byte charactors, skipped
  88.             if (ord ($s[$i]0x80) )
  89.                 continue;
  90.             $ascii_status false;
  91.  
  92.             $first KSC5601_Stream::chr2bin ($s[$i]);
  93.  
  94.             # first byte of utf8 is must start 11
  95.             if substr ($first02== '10' )
  96.                 return false;
  97.  
  98.             # except 1st byte
  99.             $byte strlen (preg_replace ('/^([1]+).*/''\\1'$first));
  100.  
  101.             if $byte )
  102.                 continue;
  103.  
  104.             /*
  105.              * 2 byte UTF-8 check is skip, because some hangle is over wrapping 2byte utf-8
  106.              */
  107.             if $byte )
  108.                 continue;
  109.  
  110.             /*
  111.              * 2byte: 1100000x (10xxxxxx)
  112.              * 3byte: 11100000 10xxxxxx (10xxxxxx)
  113.              * 4byte: 11110000 10xxxxxx (10xxxxxx 10xxxxxx)
  114.              * 5byte: 11111000 10xxxxxx (10xxxxxx 10xxxxxx 10xxxxxx)
  115.              * 6byte: 11111100 10xxx0xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx)
  116.              */
  117.             for $j=1$j<$byte$j++ {
  118.                 /*
  119.                 if ( $j == 1 ) {
  120.                     $n = 8 - $byte;
  121.                     if ( KSC5601_Stream::chr2bin ($s[$i+1], ">>$n") != KSC5601_Stream::check2byte ($byte) )
  122.                         return false;
  123.  
  124.                     continue;
  125.                 }
  126.                  */
  127.  
  128.                 if KSC5601_Stream::chr2bin ($s[$i+$j]'>>6'!= 10 )
  129.                     return false;
  130.             }
  131.  
  132.             break;
  133.         }
  134.  
  135.         if $ascii && $ascii_status )
  136.             return false;
  137.  
  138.         return true;
  139.     }
  140.     // }}}
  141.  
  142.     // {{{ function utf8enc ($s)
  143.     /**
  144.      * convert UCH to UTF-8
  145.      *
  146.      * @access public
  147.      * @return string UTF-8 strings
  148.      * @param  string Given UHC strings
  149.      */
  150.     function utf8enc ($s{
  151.         $len strlen ($s);
  152.  
  153.         for $i=0$i<$len$i++ {
  154.             if ord ($s[$i]0x80 {
  155.                 $c1 $s[$i];
  156.                 $c2 $s[$i+1];
  157.                 $ucs2 $this->ksc2ucs ($c1$c2);
  158.  
  159.                 if $ucs2 == '?' {
  160.                     $r .= $ucs2;
  161.                     $i++;
  162.                     continue;
  163.                 }
  164.  
  165.                 $uni[0$this->decbin ($ucs2 >> 12);
  166.                 $uni[1$this->decbin ($ucs2 >> 0x0f);
  167.                 $uni[2$this->decbin ($ucs2 >> 0x00f);
  168.                 $uni[3$this->decbin ($ucs2 0x000f);
  169.  
  170.                 $uc1 bindec ('1110' $uni[0]);
  171.                 $uc2 bindec ('10' $uni[1substr ($uni[2]02));
  172.                 $uc3 bindec ('10' substr ($uni[2]22$uni[3]);
  173.  
  174.                 $r .= chr ($uc1chr ($uc2chr ($uc3);
  175.                 $i++;
  176.             else
  177.                 $r .= utf8_encode ($s[$i]);
  178.         }
  179.  
  180.         return $r;
  181.     }
  182.     // }}}
  183.  
  184.     // {{{ function utf8dec ($s)
  185.     /**
  186.      * Convert UTF-8 to UHC
  187.      *
  188.      * @access public
  189.      * @return string UHC strings
  190.      * @param  string Given UTF-8 strings
  191.      */
  192.     function utf8dec ($s{
  193.         $s $this->rm_utf8bom ($s);
  194.         $l strlen ($s);
  195.  
  196.         for $i=0$i<$l$i++ {
  197.             if ord ($s[$i]0x80 {
  198.                 $uni1 ord ($s[$i]);
  199.                 $uni2 ord ($s[$i 1]);
  200.                 $uni3 ord ($s[$i 2]);
  201.  
  202.                 # 0x03 -> 00000011
  203.                 # 0x30 -> 00110000
  204.                 $ucs2 dechex ($uni1 0x0f.
  205.                         dechex ($uni2 >> 0x0f.
  206.                         dechex ((($uni2 0x03<<2(($uni3 0x30>> 4)) .
  207.                         dechex ($uni3 0x0f);
  208.  
  209.                 if $this->debug {
  210.                     #     ucs0     ucs1  ucs2       ucs3
  211.                     #1111(1111).11(1111)(11).11(11)(1111)
  212.                     echo 'HEX STR => ' $ucs2 "\n";
  213.                     echo '0 => ' $ucs2[0' ' decbin (hexdec ($ucs2[0])) "\n";
  214.                     echo '1 => ' $ucs2[1' ' decbin (hexdec ($ucs2[1])) "\n";
  215.                     echo '2 => ' $ucs2[2' ' decbin (hexdec ($ucs2[2])) "\n";
  216.                     echo '3 => ' $ucs2[3' ' decbin (hexdec ($ucs2[3])) "\n";
  217.                 }
  218.  
  219.                 $r .= $this->ucs2ksc ($ucs2);
  220.  
  221.                 $i += 2;
  222.             else
  223.                 $r .= utf8_decode ($s[$i]);
  224.         }
  225.  
  226.         return $r;
  227.     }
  228.     // }}}
  229. }
  230.  
  231. /*
  232.  * Local variables:
  233.  * tab-width: 4
  234.  * c-basic-offset: 4
  235.  * End:
  236.  * vim600: noet sw=4 ts=4 fdm=marker
  237.  * vim<600: noet sw=4 ts=4
  238.  */
  239. ?>

Documentation generated on Tue, 14 May 2019 02:00:46 +0900 by phpDocumentor 1.4.4