Source for file KSC5601_ext.php
Documentation is available at KSC5601_ext.php
* Project: KSC5601 :: convert character set between KSC5601 and UTF8
* File: KSC5601/KSC5601_ext.php
* Sub pcakge of KSC5601 package. This package is used when php support
* iconv or mbstring extensions.
* @subpackage KSC5601_ext
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2015, JoungKyun.Kim
* @link http://pear.oops.org/package/KSC5601
* Define EXTMODE to true. This means that php support iconv or mbstring
* import original API of KSC5601_ext::is_utf8 for check whether
require_once 'KSC5601/UTF8.php';
* Original API of KSC5601 that used iconv or mbstring.
* Status whether process hangul that is out of ksx1001 range.
* Set false, no action for hangul that is out of ksx1001 range.
// {{{ public (void) __construct ($is_ext)
* Support iconv or mbstring extension, use KSC5601_ext internal class, or not
* support use KSC5601_pure internal class.
* @param KSC5601_Common return value of KSC5601_Common class
// {{{ public (bool) out_of_ksx1001 ($flag = false)
* Set whether convert hangul that is out of KSX1001 range. This method changes
* private $out_ksc1001 variable.
* @param boolean (optional) Defaults to false
* <li>true : When decode UTF-8, convert to NCR from hangul character that is out of KSX1001 range.</li>
* <li>true : When encode NCR from UHC(CP949), convert to NCR with only hangul that is out of KSX1001 range.</li>
* <li>false : No action</li>
// {{{ public (bool) is_utf8 ($string, $ascii = false)
* Check given string wheter utf8 of not.
* @return boolean Given string is utf8, return true.
* @param string Given strings
* @param boolean Check whether is ascii only or not
function is_utf8 ($string, $ascii = false) {
return parent::is_utf8 ($string, $ascii);
// {{{ public (string) utf8 ($string, $to = UTF8)
* Convert between UHC and UTF-8
* @param string Given string.
* @param string (optional) Defaults to UTF8. Value is UTF8 or UHC constant.
* This parameter is not set or set with UTF8 constant, convert
* Set to UHC constant, conert to uhc from utf-8. If intenal
* $out_ksx1001 variable is set true that means call
* KSC5601::out_of_ksx1001(true)), convert to NCR hangul
* that is out of KSX1001 range.
* @see KSC5601::out_of_ksx1001
function utf8 ($string, $to = UTF8) {
$string = $this->ncr ($string, UHC);
$r = $this->obj->extfunc ($from, $to, $string);
// {{{ public (string) ucs2 ($string, $to = UCS2, $asc = false)
* Convert between UHC and UCS2
* @param string Given string
* @param string (optional) Detauls to UCS2. Value is UCS2 or UHC constants.
* Set UCS2 constant, convert UHC to UCS2 hexical (for example, U+B620).
* Set UHC constant, convert UCS2 hexical to UHC.
* @param boolean (optional) Defaults to false. This parameter is used only UHC -> UCS2 mode.
* Set true, convert all characters to UCS2 hexical. Set false, only convert
* hangul that is out of KSX1001 range to UCS hexical.
function ucs2 ($string, $to = UCS2, $asc = false) {
return $this->ucs2enc ($string, $asc);
return $this->ucs2dec ($string);
// {{{ private (string) ucs2enc ($string, $asc = false)
* Convert UHC to UCS2 hexical
* @param string Given String
* @param boolean (optional) Defaults to false.
private function ucs2enc ($string, $asc = false) {
$string = $this->obj->extfunc (UHC, UCS2, $string);
for ( $i= 0; $i< $l; $i++ ) {
if ( ord ($string[$i]) == 0 ) {
$r .= ( $asc === false ) ?
// {{{ private (string) ucs2dec ($string)
* Convert UCS2 hexical to UHC
* @param string Given String
private function ucs2dec ($string) {
\$r = chr (hexdec (\$matches[1])) . chr (hexdec (\$matches[2]));
\$r = chr (0) . chr (hexdec (\$matches[1]));
if ( extension_loaded ('iconv') )
return iconv (UCS2, UHC, \$r);
else if ( extension_loaded ('mbstring') )
return mb_convert_encoding (\$r, UHC, UCS2);
// {{{ public (string) ncr ($string, $to = NCR, $enc = false)
* Convert between UHC and NCR (Numeric Code Reference)
* @param string Given string
* @param string (optional) Defaults to NCR constant. Value is NCR or UHC constants.
* Set NCR constant, convert UHC(CP949) to NCR code. Set UHC constant,
* convert NCR code to UHC(cp949).
* @param boolean (optional) Defaults to false. This parameter is used only UHC -> NCR mode.
* Set false, only convert hangul that is out of KSX1001 range to NCR
* when internal $out_ksx1001 variable set true that meas called
* KSC5601::out_of_ksx1001 (true).
* Set true, convert all character to NCR code.
function ncr ($string, $to = NCR, $enc = false) {
return $this->ncr2enc ($string, $enc);
return $this->ncr2dec ($string);
// {{{ private (string) ncr2enc ($string, $enc = false)
* Convert NCR code to UCS2
* @param string Given String that is conscruct with NCR code or included NCR code.
* @param boolena (optional) Defaults to false.
private function ncr2enc ($string, $enc = false) {
for ( $i= 0; $i< $l; $i++ ) {
$u = $this->obj->extfunc (UHC, UCS2, $string[$i- 1] . $string[$i]);
$r .= $string[$i- 1] . $string[$i];
$u = $this->obj->extfunc (UHC, UCS2, $string[$i- 1] . $string[$i]);
$string = $this->obj->extfunc (UHC, UCS2, $string);
for ( $i= 0; $i< $l; $i++ ) {
if ( ord ($string[$i]) == 0 ) {
// {{{ private (string) ncr2dec ($string)
* Convert NCR code to UHC
* @param string Given string
private function ncr2dec ($string) {
\$m[1] = ( \$m[1][0] == 'x' ) ? substr (\$m[1], 1) : dechex (\$m[1]);
if ( strlen (\$m[1]) % 2 )
preg_match ('/^([[:alnum:]]{2})([[:alnum:]]{2})?$/', \$m[1], \$matches);
\$n = chr (hexdec (\$matches[1]));
\$n .= chr (hexdec (\$matches[2]));
if ( extension_loaded ('iconv') )
return iconv ('ucs-2be', 'uhc', \$n);
else if ( extension_loaded ('mbstring') )
return mb_convert_encoding (\$n, 'uhc', 'ucs-2be');
if ( extension_loaded ('iconv') )
return iconv ('ucs-2', 'uhc', \$n);
else if ( extension_loaded ('mbstring') )
return mb_convert_encoding (\$n, 'uhc', 'ucs-2');
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|