function htmlEncode($html, $in = null, $out = null, $mode = 'auto')
    {
        $valid = array(
            'auto',
            'iconv',
            'mb_convert_encoding'
        );
        if (! isset($out)) {
            $out = 'UTF-8';
        }
        if (! in_array($mode, $valid)) {
            user_error('invalid mode, mode=' . $mode, E_USER_ERROR);
        }
        $if = function_exists('mb_convert_encoding');
        $if = $if && ($mode == 'auto' || $mode == 'mb_convert_encoding');
        $func = null;
        if (function_exists('iconv') && ($mode == 'auto' || $mode == 'iconv')) {
            $func = 'iconv';
        } elseif ($if) {
            $func = 'mb_convert_encoding';
        } else {
            user_error('encode failed, php extension not found', E_USER_ERROR);
        }
        $pattern = '/(<meta[^>]*?charset=(["\']?))([a-z\d_\-]*)(\2[^>]*?>)/is';
        if (! isset($in)) {
            $n = preg_match($pattern, $html, $in);
            if ($n > 0) {
                $in = $in[3];
            } else {
                if (function_exists('mb_detect_encoding')) {
                    $in = mb_detect_encoding($html);
                } else {
                    $in = null;
                }
            }
        }
        if (isset($in)) {
            $old = error_reporting(error_reporting() & ~ E_NOTICE);
            $html = call_user_func($func, $in, $out . '//IGNORE', $html);
            error_reporting($old);
            $html = preg_replace($pattern, "\\1$out\\4", $html, 1);
        } else {
            user_error('source encoding is unknown', E_USER_ERROR);
        }
        return $html;
    }
function htmlEncode($html, $in = null, $out = null, $mode = 'auto')
    {
        $valid = array(
            'auto',
            'iconv',
            'mb_convert_encoding'
        );
        if (! isset($out)) {
            $out = 'UTF-8';
        }
        if (! in_array($mode, $valid)) {
            user_error('invalid mode, mode=' . $mode, E_USER_ERROR);
        }
        $if = function_exists('mb_convert_encoding');
        $if = $if && ($mode == 'auto' || $mode == 'mb_convert_encoding');
        $func = null;
        if (function_exists('iconv') && ($mode == 'auto' || $mode == 'iconv')) {
            $func = 'iconv';
        } elseif ($if) {
            $func = 'mb_convert_encoding';
        } else {
            user_error('encode failed, php extension not found', E_USER_ERROR);
        }
        $pattern = '/(<meta[^>]*?charset=(["\']?))([a-z\d_\-]*)(\2[^>]*?>)/is';
        if (! isset($in)) {
            $n = preg_match($pattern, $html, $in);
            if ($n > 0) {
                $in = $in[3];
            } else {
                if (function_exists('mb_detect_encoding')) {
                    $in = mb_detect_encoding($html);
                } else {
                    $in = null;
                }
            }
        }
        if (isset($in)) {
            $old = error_reporting(error_reporting() & ~ E_NOTICE);
            $html = call_user_func($func, $in, $out . '//IGNORE', $html);
            error_reporting($old);
            $html = preg_replace($pattern, "\\1$out\\4", $html, 1);
        } else {
            user_error('source encoding is unknown', E_USER_ERROR);
        }
        return $html;
    }
function htmlEncode ( $ html , $ in = null , $ out = null , $ mode = 'auto' ) { $ valid = array ( 'auto' , 'iconv' , 'mb_convert_encoding' ) ; if ( ! isset ( $ out ) ) { $ out = 'UTF-8' ; } if ( ! in_array ( $ mode , $ valid ) ) { user_error ( 'invalid mode, mode=' . $ mode , E_USER_ERROR ) ; } $ if = function_exists ( 'mb_convert_encoding' ) ; $ if = $ if && ( $ mode == 'auto' || $ mode == 'mb_convert_encoding' ) ; $ func = null ; if ( function_exists ( 'iconv' ) && ( $ mode == 'auto' || $ mode == 'iconv' ) ) { $ func = 'iconv' ; } elseif ( $ if ) { $ func = 'mb_convert_encoding' ; } else { user_error ( 'encode failed, php extension not found' , E_USER_ERROR ) ; } $ pattern = '/(<meta[^>]*?charset=(["\']?))([a-z\d_\-]*)(\2[^>]*?>)/is' ; if ( ! isset ( $ in ) ) { $ n = preg_match ( $ pattern , $ html , $ in ) ; if ( $ n > 0 ) { $ in = $ in [ 3 ] ; } else { if ( function_exists ( 'mb_detect_encoding' ) ) { $ in = mb_detect_encoding ( $ html ) ; } else { $ in = null ; } } } if ( isset ( $ in ) ) { $ old = error_reporting ( error_reporting ( ) & ~ E_NOTICE ) ; $ html = call_user_func ( $ func , $ in , $ out . '//IGNORE' , $ html ) ; error_reporting ( $ old ) ; $ html = preg_replace ( $ pattern , "\\1$out\\4" , $ html , 1 ) ; } else { user_error ( 'source encoding is unknown' , E_USER_ERROR ) ; } return $ html ; }
Html encoding transform

@param string $html
@param string $in
detect automatically if not set
@param string $out
default UTF-8
@param string $mode
auto|iconv|mb_convert_encoding
@return string
Html encoding transform