Example #1
0
function charset2unicode($texte, $charset='AUTO' /* $forcer: obsolete*/) {
	static $trans;

	if ($charset == 'AUTO')
		$charset = $GLOBALS['meta']['charset'];

	if ($charset == '') $charset = 'iso-8859-1';
	$charset = strtolower($charset);

	switch ($charset) {
	case 'utf-8':
	case 'utf8':
		return utf_8_to_unicode($texte);

	case 'iso-8859-1':
		$texte = corriger_caracteres_windows($texte, 'iso-8859-1');
		// pas de break; ici, on suit sur default:

	default:
		// mbstring presente ?
		if (init_mb_string()) {
			if ($order = mb_detect_order() # mb_string connait-il $charset?
			AND mb_detect_order($charset)) {
				$s = mb_convert_encoding($texte, 'utf-8', $charset);
				if ($s && $s != $texte) return utf_8_to_unicode($s);
			}
			mb_detect_order($order); # remettre comme precedemment
		}

		// Sinon, peut-etre connaissons-nous ce charset ?
		if (!isset($trans[$charset])) {
			global $CHARSET;
			if ($cset = load_charset($charset)
			AND is_array($CHARSET[$cset]))
				foreach ($CHARSET[$cset] as $key => $val) {
					$trans[$charset][chr($key)] = '&#'.$val.';';
			}
		}
		if (count($trans[$charset]))
			return str_replace(array_keys($trans[$charset]),array_values($trans[$charset]),$texte);

		// Sinon demander a iconv (malgre le fait qu'il coupe quand un
		// caractere n'appartient pas au charset, mais c'est un probleme
		// surtout en utf-8, gere ci-dessus)
		if (test_iconv()) {
			$s = iconv($charset, 'utf-32le', $texte);
			if ($s) return utf_32_to_unicode($s);
		}

		// Au pire ne rien faire
		spip_log("erreur charset '$charset' non supporte");
		return $texte;
	}
}
Example #2
0
function charset2unicode($texte, $charset = 'AUTO', $forcer = false)
{
    static $trans;
    if ($charset == 'AUTO') {
        $charset = read_meta('charset');
    }
    $charset = strtolower($charset);
    switch ($charset) {
        case 'utf-8':
            // Le passage par utf-32 devrait etre plus rapide
            // (traitements PHP reduits au minimum)
            if (test_iconv()) {
                $s = iconv('utf-8', 'utf-32', $texte);
                if ($s) {
                    return utf_32_to_unicode($s);
                }
            }
            return utf_8_to_unicode($texte);
        case 'iso-8859-1':
            // On commente cet appel tant qu'il reste des spip v<1.5 dans la nature
            // pour que le filtre |entites_unicode donne des backends lisibles sur ces spips.
            if (!$forcer) {
                return $texte;
            }
        default:
            if (test_iconv()) {
                $s = iconv($charset, 'utf-32', $texte);
                if ($s) {
                    return utf_32_to_unicode($s);
                }
            }
            if (!$trans[$charset]) {
                global $CHARSET;
                load_charset($charset);
                reset($CHARSET[$charset]);
                while (list($key, $val) = each($CHARSET[$charset])) {
                    $trans[$charset][chr($key)] = '&#' . $val . ';';
                }
            }
            if ($trans[$charset]) {
                if ($GLOBALS['flag_strtr2']) {
                    $texte = strtr($texte, $trans[$charset]);
                } else {
                    reset($trans[$charset]);
                    while (list($from, $to) = each($trans[$charset])) {
                        $texte = str_replace($from, $to, $texte);
                    }
                }
            }
            return $texte;
    }
}