Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

Commit

Permalink
Make translate_encoding generic
Browse files Browse the repository at this point in the history
Will make easier the implementation of marekrei#40 and later fixing of marekrei#37
  • Loading branch information
NewEraCracker committed Apr 5, 2016
1 parent e81327b commit efc8aa1
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2819,20 +2819,30 @@ function debug()
}

//
// Encode HTML output in correct encoding
// Encode output in correct encoding
//
public static function translate_encoding($string)
public static function translate_encoding($string, $undo = false)
{
if(!is_string($string) || EncodeExplorer::getConfig('charset') == EncodeExplorer::getConfig('os_charset'))
return $string;

if(!$undo) {
// From system encoding to output
$in = EncodeExplorer::getConfig('os_charset');
$out = EncodeExplorer::getConfig('charset');
} else {
// From input to system encoding
$in = EncodeExplorer::getConfig('charset');
$out = EncodeExplorer::getConfig('os_charset');
}

// Attempt using mb_convert_encoding
if(function_exists('mb_convert_encoding'))
$tmp = @mb_convert_encoding($string, EncodeExplorer::getConfig('charset'), EncodeExplorer::getConfig('os_charset'));
$tmp = @mb_convert_encoding($string, $out, $in);

// Attempt using iconv
if(empty($tmp) && function_exists('iconv'))
$tmp = @iconv(EncodeExplorer::getConfig('os_charset'), EncodeExplorer::getConfig('charset'), $string);
$tmp = @iconv($in, $out, $string);

// If any of them succeeds, return converted one
if(!empty($tmp) && is_string($tmp))
Expand Down

0 comments on commit efc8aa1

Please sign in to comment.