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

Source for file KSC5601_ext.php

Documentation is available at KSC5601_ext.php

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

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