Source for file KSC5601_pure.php
Documentation is available at KSC5601_pure.php
* Project: KSC5601 :: convert character set between KSC5601 and UTF8
* File: KSC5601/KSC5601_pure.php
* Sub pcakge of KSC5601 package. This package is used when php don't support
* iconv or mbstring extensions.
* @subpackage KSC5601_pure
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2015, JoungKyun.Kim
* @link http://pear.oops.org/package/KSC5601
* Define EXTMODE to false. This means that php don't support iconv or mbstring
* import UTF-8 API that has pure php code for UTF-8
require_once 'KSC5601/UTF8.php';
* Original API of KSC5601 that used pure php code
* Status whether process hangul that is out of ksx1001 range.
* Set false, no action for hangul that is out of ksx1001 range.
// {{{ function 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>
// {{{ function is_utf8 ($string, $ascii)
* 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);
// {{{ function 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) {
// {{{ function 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 function 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) {
for ( $i= 0; $i< $l; $i++ ) {
if ( ord ($string[$i]) & 0x80 ) {
$r .= 'U+' . strtoupper (dechex (parent::ksc2ucs ($string[$i], $string[$i+ 1])));
# $asc == true, don't convert ascii code to NCR code
$r .= ( $asc === false ) ? $string[$i] : 'U+' . parent::chr2hex ($string[$i], false);
// {{{ private function ucs2dec ($string)
* Convert UCS2 hexical to UHC
* @param string Given String
private function ucs2dec ($string) {
for ( $i= 0; $i< $l; $i++ ) {
if ( $s[$i] == 'U' && $s[$i + 1] == '+' ) {
while ( $s[$i] != 'U' && $i < $l ) {
$r .= parent::ucs2ksc ($c);
// {{{ function 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 function 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++ ) {
if ( ord ($string[$i]) & 0x80 ) {
$hex = dechex (parent::ksc2ucs ($string[$i], $string[$i+ 1]));
# $enc == true, don't convert ascii code to NCR code
$hex = 'x' . parent::chr2hex ($string[$i], false);
for ( $i= 0; $i< $l; $i++ ) {
if ( ord ($string[$i]) & 0x80 ) {
if ( parent::is_out_of_ksx1001 ($string[$i- 1], $string[$i]) ) {
$hex = dechex (parent::ksc2ucs ($string[$i- 1], $string[$i]));
$r .= $string[$i- 1] . $string[$i];
$hex = dechex (parent::ksc2ucs ($string[$i- 1], $string[$i]));
// {{{ private function ncr2dec ($string)
* Convert NCR code to UHC
* @param string Given string
private function ncr2dec ($str) {
for ( $i= 0; $i< $l; $i++ ) {
if ( $str[$i] == '&' && $str[$i + 1] == '#' ) {
if ( $str[$i + 3] == ';' ) {
} else if ( $str[$i + 4] == ';' ) {
$c = $str[$i + 2] . $str[$i + 3];
} else if ( $str[$i + 5] == ';' ) {
$c = $str[$i + 2] . $str[$i + 3] . $str[$i + 4];
} else if ( $str[$i + 6] == ';' ) {
$c = $str[$i + 2] . $str[$i + 3] . $str[$i + 4] . $str[$i + 5];
} else if ( $str[$i + 7] == ';' ) {
$c = $str[$i + 2] . $str[$i + 3] . $str[$i + 4] . $str[$i + 5] . $str[$i + 6];
$r .= parent::ucs2ksc ($c);
// {{{ function make_reverse_table ()
* Print php code for KSC5601 reverse table
* This method is used only developer for KSC5601 pure code.
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|