/** * Convert a string between charsets * * @access public * @param string Input String * @param string Current char set * @param string Destination char set * @return string Parsed string * @since 2.1.0 * @todo [Future] If an error is set in classConvertCharset, show it or log it somehow */ public static function convertCharsets($text, $original_cset, $destination_cset = "UTF-8") { $original_cset = strtolower($original_cset); $t = $text; //----------------------------------------- // Not the same? //----------------------------------------- if ($destination_cset == $original_cset) { return $t; } if (!is_object(self::$classConvertCharset)) { require_once IPS_KERNEL_PATH . '/classConvertCharset.php'; self::$classConvertCharset = new classConvertCharset(); //if ( function_exists( 'mb_convert_encoding' ) ) //{ // self::$classConvertCharset->method = 'mb'; //} //else if ( function_exists( 'iconv' ) ) //{ // self::$classConvertCharset->method = 'iconv'; //} //else if ( function_exists( 'recode_string' ) ) //{ // self::$classConvertCharset->method = 'recode'; //} //else //{ self::$classConvertCharset->method = 'internal'; //} } $text = self::$classConvertCharset->convertEncoding($text, $original_cset, $destination_cset); return $text ? $text : $t; }
/** * Convert a string between charsets * * @param string Input String * @param string Current char set * @param string Destination char set * @return string Parsed string * @since 2.1.0 * @todo [Future] If an error is set in classConvertCharset, show it or log it somehow */ public static function convertCharsets($text, $original_cset, $destination_cset = "UTF-8") { define('CONVERT_JSU_TO_ENTITY', true); $original_cset = strtolower($original_cset); $destination_cset = strtolower($destination_cset); $t = $text; //----------------------------------------- // Not the same? //----------------------------------------- if ($destination_cset == $original_cset) { return $t; } //----------------------------------------- // Are these only latin chars? If so, no conversion should be needed. //----------------------------------------- if (preg_match('/^([a-zA-Z0-9_\\-\\.:;\\[\\]]+?)$/', $t)) { return $text; } if (!is_object(self::$classConvertCharset)) { require_once IPS_KERNEL_PATH . '/classConvertCharset.php'; /*noLibHook*/ self::$classConvertCharset = new classConvertCharset(); if (ipsRegistry::$settings['charset_conv_method'] == 'mb' and function_exists('mb_convert_encoding')) { self::$classConvertCharset->method = 'mb'; } else { if (ipsRegistry::$settings['charset_conv_method'] == 'iconv' and function_exists('iconv')) { self::$classConvertCharset->method = 'iconv'; } else { if (ipsRegistry::$settings['charset_conv_method'] == 'recode' and function_exists('recode_string')) { self::$classConvertCharset->method = 'recode'; } else { self::$classConvertCharset->method = 'internal'; } } } } $text = self::$classConvertCharset->convertEncoding($text, $original_cset, $destination_cset); /* Experimental - convert \u#### to html entity */ if ($destination_cset != 'utf-8' and $text and CONVERT_JSU_TO_ENTITY) { preg_match_all('#\\\\u([0-9]{4})#ims', $text, $matches); if (is_array($matches) and count($matches)) { foreach ($matches[1] as $k => $v) { $v = "&#" . hexdec(ltrim($v, '0')) . ";"; $text = str_replace($matches[0][$k], $v, $text); } } } return $text ? $text : $t; }