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

Source for file KSC5601_pure.php

Documentation is available at KSC5601_pure.php

  1. <?php
  2. /**
  3.  * Project: KSC5601 :: convert character set between KSC5601 and UTF8
  4.  * File:    KSC5601/KSC5601_pure.php
  5.  * 
  6.  * Sub pcakge of KSC5601 package. This package is used when php don't support
  7.  * iconv or mbstring extensions.
  8.  *
  9.  * @category   Charset
  10.  * @package    KSC5601
  11.  * @subpackage KSC5601_pure
  12.  * @author     JoungKyun.Kim <http://oops.org>
  13.  * @copyright  (c) 2015, JoungKyun.Kim
  14.  * @license    BSD License
  15.  * @version    $Id$
  16.  * @link       http://pear.oops.org/package/KSC5601
  17.  * @filesource
  18.  */
  19.  
  20. // {{{ constant
  21. /**
  22.  * Define EXTMODE to false. This means that php don't support iconv or mbstring
  23.  * extensions.
  24.  */
  25. define ('EXTMODE',    false);
  26. // }}}
  27.  
  28. /**
  29.  * import UTF-8 API that has pure php code for UTF-8
  30.  */
  31. require_once 'KSC5601/UTF8.php';
  32.  
  33.  
  34. /**
  35.  * Original API of KSC5601 that used pure php code
  36.  *
  37.  * @package KSC5601
  38.  */
  39. Class KSC5601_pure extends KSC5601_UTF8
  40. {
  41.     // {{{ properties
  42.     /**#@+
  43.      * @access private
  44.      */
  45.     /**
  46.      * KSC5601_UTF8 object
  47.      * @var KSC5601_UTF8 
  48.      */
  49.     private $obj;
  50.     /**
  51.      * Status whether process hangul that is out of ksx1001 range.
  52.      * Set false, no action for hangul that is out of ksx1001 range.
  53.      * @var boolean 
  54.      */
  55.     protected $out_ksx1001 = false;
  56.     /**#@-*/
  57.     // }}}
  58.  
  59.     // {{{ construct
  60.     /**
  61.      * @access public
  62.      * @return vold 
  63.      */
  64.     function __construct ({
  65.         $this->obj new KSC5601_UTF8;
  66.     }
  67.     // }}}
  68.  
  69.     // {{{ function out_of_ksx1001 ($flag = false)
  70.     /**
  71.      * Set whether convert hangul that is out of KSX1001 range. This method changes
  72.      * private $out_ksc1001 variable.
  73.      *
  74.      * @access  public
  75.      * @return  boolean Return
  76.      * @param   boolean (optional) Defaults to false
  77.      *   <ol>
  78.      *       <li>true : When decode UTF-8, convert to NCR from hangul character that is out of KSX1001 range.</li>
  79.      *       <li>true : When encode NCR from UHC(CP949), convert to NCR with only hangul that is out of KSX1001 range.</li>
  80.      *       <li>false : No action</li>
  81.      *   </ol>
  82.      */
  83.     function out_of_ksx1001 ($flag false{
  84.         $this->out_ksx1001 = $flag;
  85.         return $this->out_ksx1001;
  86.     }
  87.     // }}}
  88.  
  89.     // {{{ function is_utf8 ($string, $ascii)
  90.     /**
  91.      * Check given string wheter utf8 of not.
  92.      *
  93.      * @access  public
  94.      * @return  boolean Given string is utf8, return true.
  95.      * @param   string  Given strings
  96.      * @param   boolean Check whether is ascii only or not
  97.      */
  98.     function is_utf8 ($string$ascii false{
  99.         return parent::is_utf8 ($string$ascii);
  100.     }
  101.     // }}}
  102.  
  103.     // {{{ function utf8 ($string, $to = UTF8)
  104.     /**
  105.      * Convert between UHC and UTF-8
  106.      *
  107.      * @access  public
  108.      * @return  string 
  109.      * @param   string  Given string.
  110.      * @param   string  (optional) Defaults to UTF8. Value is UTF8 or UHC constant.
  111.      *                   This parameter is not set or set with UTF8 constant, convert
  112.      *                   given string to UTF-8.
  113.      *
  114.      *                   Set to UHC constant, conert to uhc from utf-8. If intenal
  115.      *                   $out_ksx1001 variable is set true that means call
  116.      *                   KSC5601::out_of_ksx1001(true)), convert to NCR hangul
  117.      *                   that is out of KSX1001 range.
  118.      *                  @see KSC5601::out_of_ksx1001
  119.      */
  120.     function utf8 ($string$to UTF8{
  121.         if $to === UTF8 )
  122.             return parent::utf8enc ($string);
  123.  
  124.         return parent::utf8dec ($string);
  125.     }
  126.     // }}}
  127.  
  128.     // {{{ function ucs2 ($string, $to = UCS2, $asc = false)
  129.     /**
  130.      * Convert between UHC and UCS2
  131.      *
  132.      * @access  public
  133.      * @return  string 
  134.      * @param   string  Given string
  135.      * @param   string  (optional) Detauls to UCS2. Value is UCS2 or UHC constants.
  136.      *                   Set UCS2 constant, convert UHC to UCS2 hexical (for example, U+B620).
  137.      *                   Set UHC constant, convert UCS2 hexical to UHC.
  138.      * @param   boolean (optional) Defaults to false. This parameter is used only UHC -> UCS2 mode.
  139.      *                   Set true, convert all characters to UCS2 hexical. Set false, only convert
  140.      *                   hangul that is out of KSX1001 range to UCS hexical.
  141.      */
  142.     function ucs2 ($string$to UCS2$asc false{
  143.         if preg_match ('/ucs[-]?2(be|le)?/i'$to) ) {
  144.             /* to ucs2 */
  145.             return $this->ucs2enc ($string$asc);
  146.         else {
  147.             /* to UHC */
  148.             return $this->ucs2dec ($string);
  149.         }
  150.     }
  151.     // }}}
  152.  
  153.     // {{{ private function ucs2enc ($string, $asc = false)
  154.     /**
  155.      * Convert UHC to UCS2 hexical
  156.      *
  157.      * @access  private
  158.      * @return  string 
  159.      * @param   string  Given String
  160.      * @param   boolean (optional) Defaults to false.
  161.      */
  162.     private function ucs2enc ($string$asc false{
  163.         $l strlen ($string);
  164.  
  165.         for $i=0$i<$l$i++ {
  166.             if ord ($string[$i]0x80 {
  167.                 $r .= 'U+' strtoupper (dechex (parent::ksc2ucs ($string[$i]$string[$i+1])));
  168.                 $i++;
  169.             else {
  170.                 # $asc == true, don't convert ascii code to NCR code
  171.                 $r .= $asc === false $string[$i'U+' parent::chr2hex ($string[$i]false);
  172.             }
  173.         }
  174.  
  175.         return $r;
  176.     }
  177.     // }}}
  178.  
  179.     // {{{ private function ucs2dec ($string)
  180.     /**
  181.      * Convert UCS2 hexical to UHC
  182.      *
  183.      * @access private
  184.      * @return string 
  185.      * @param  string Given String
  186.      */
  187.     private function ucs2dec ($string{
  188.         $s preg_replace ('/0x([a-z0-9]{2,4})/i''U+\\1'trim ($string));
  189.  
  190.         $l strlen ($s);
  191.  
  192.         for $i=0$i<$l$i++ {
  193.             if $s[$i== 'U' && $s[$i 1== '+' {
  194.                 $i += 2;
  195.                 $c '';
  196.                 while $s[$i!= 'U' && $i $l {
  197.                     $c .= $s[$i++];
  198.  
  199.                     if strlen ($c== )
  200.                         break;
  201.                 }
  202.                 $i--;
  203.  
  204.                 if strlen ($c== )
  205.                     $r .= parent::ucs2ksc ($c);
  206.                 else
  207.                     $r .= chr (hexdec ($c));
  208.             else
  209.                 $r .= $s[$i];
  210.         }
  211.  
  212.         return $r;
  213.     }
  214.     // }}}
  215.  
  216.     // {{{ function ncr ($string, $to = NCR, $enc = false)
  217.     /**
  218.      * Convert between UHC and NCR (Numeric Code Reference)
  219.      *
  220.      * @access  public
  221.      * @return  string 
  222.      * @param   string  Given string
  223.      * @param   string  (optional) Defaults to NCR constant. Value is NCR or UHC constants.
  224.      *                   Set NCR constant, convert UHC(CP949) to NCR code. Set UHC constant,
  225.      *                   convert NCR code to UHC(cp949).
  226.      * @param   boolean (optional) Defaults to false. This parameter is used only UHC -> NCR mode.
  227.      *                   Set false, only convert hangul that is out of KSX1001 range to NCR
  228.      *                   when internal $out_ksx1001 variable set true that meas called
  229.      *                   KSC5601::out_of_ksx1001 (true).
  230.      *
  231.      *                   Set true, convert all character to NCR code.
  232.      */
  233.     function ncr ($string$to NCR$enc false{
  234.         if $to == NCR {
  235.             /* to ucs2 */
  236.             return $this->ncr2enc ($string$enc);
  237.         else {
  238.             /* to UHC */
  239.             return $this->ncr2dec ($string);
  240.         }
  241.     }
  242.     // }}}
  243.  
  244.     // {{{ private function ncr2enc ($string, $enc = false)
  245.     /**
  246.      * Convert NCR code to UCS2
  247.      *
  248.      * @access private
  249.      * @return string 
  250.      * @param  string  Given String that is conscruct with NCR code or included NCR code.
  251.      * @param  boolena (optional) Defaults to false.
  252.      */
  253.     private function ncr2enc ($string$enc false{
  254.         $l strlen ($string);
  255.  
  256.         if $enc === true {
  257.             for $i=0$i<$l$i++ {
  258.                 if ord ($string[$i]0x80 {
  259.                     $hex dechex (parent::ksc2ucs ($string[$i]$string[$i+1]));
  260.                     $hex 'x' strtoupper ($hex);
  261.                     $r .= '&#' $hex ';';
  262.                     $i++;
  263.                 else {
  264.                     # $enc == true, don't convert ascii code to NCR code
  265.                     $hex 'x' parent::chr2hex ($string[$i]false);
  266.                     $r .= '&#' $hex ';';
  267.                 }
  268.             }
  269.  
  270.             return $r;
  271.         }
  272.  
  273.         for $i=0$i<$l$i++ {
  274.             if ord ($string[$i]0x80 {
  275.                 $i++;
  276.                 if $this->out_ksx1001 === true {
  277.                      if parent::is_out_of_ksx1001 ($string[$i-1]$string[$i]) ) {
  278.                         $hex dechex (parent::ksc2ucs ($string[$i-1]$string[$i]));
  279.                         $hex 'x' strtoupper ($hex);
  280.                         $r .= '&#' $hex ';';
  281.                     else
  282.                         $r .= $string[$i-1$string[$i];
  283.                 else {
  284.                     $hex dechex (parent::ksc2ucs ($string[$i-1]$string[$i]));
  285.                     $hex 'x' strtoupper ($hex);
  286.                     $r .= '&#' $hex ';';
  287.                 }
  288.             else
  289.                 $r .= $string[$i];
  290.         }
  291.  
  292.         return $r;
  293.     }
  294.     // }}}
  295.  
  296.     // {{{ private function ncr2dec ($string)
  297.     /**
  298.      * Convert NCR code to UHC
  299.      *
  300.      * @access private
  301.      * @return string 
  302.      * @param  string Given string
  303.      */
  304.     private function ncr2dec ($str{
  305.         $l strlen ($str);
  306.  
  307.         for $i=0$i<$l$i++ {
  308.             if $str[$i== '&' && $str[$i 1== '#' {
  309.                 if $str[$i 3== ';' {
  310.                     $c $str[$i 2];
  311.                     $i += 3;
  312.                 else if $str[$i 4== ';' {
  313.                     $c $str[$i 2$str[$i 3];
  314.                     $i += 4;
  315.                 else if $str[$i 5== ';' {
  316.                     $c $str[$i 2$str[$i 3$str[$i 4];
  317.                     $i += 5;
  318.                 else if $str[$i 6== ';' {
  319.                     $c $str[$i 2$str[$i 3$str[$i 4$str[$i 5];
  320.                     $i += 6;
  321.                 else if $str[$i 7== ';' {
  322.                     $c $str[$i 2$str[$i 3$str[$i 4$str[$i 5$str[$i 6];
  323.                     $i += 7;
  324.                 else {
  325.                     $r .= $str[$i];
  326.                     continue;
  327.                 }
  328.  
  329.                 if $c[0== 'x' )
  330.                     $c substr ($c1);
  331.                 else
  332.                     $c dechex ($c);
  333.  
  334.                 if strlen ($c== {
  335.                     $org_ksx1001 $this->out_ksx1001;
  336.                     $this->out_ksx1001 = false;
  337.  
  338.                     $r .= parent::ucs2ksc ($c);
  339.  
  340.                     $this->out_ksx1001 = $org_ksx1001;
  341.                 else
  342.                     $r .= chr (hexdec ($c));
  343.             else
  344.                 $r .= $str[$i];
  345.         }
  346.  
  347.         return $r;
  348.     }
  349.     // }}}
  350.  
  351.     // {{{ function make_reverse_table ()
  352.     /**
  353.      * Print php code for KSC5601 reverse table
  354.      * This method is used only developer for KSC5601 pure code.
  355.      *
  356.      * @access public
  357.      * @return void 
  358.      * @param  void 
  359.      */
  360.     function make_reverse_table ({
  361.         parent::mk_revTable ();
  362.     }
  363.     // }}}
  364. }
  365.  
  366. /*
  367.  * Local variables:
  368.  * tab-width: 4
  369.  * c-basic-offset: 4
  370.  * End:
  371.  * vim600: noet sw=4 ts=4 fdm=marker
  372.  * vim<600: noet sw=4 ts=4
  373.  */
  374. ?>

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