3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2012 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
14 * Gestion des charsets et des conversions
16 * Ce fichier contient les fonctions relatives à la gestion de charsets,
17 * à la conversion de textes dans différents charsets et
18 * propose des fonctions émulant la librairie mb si elle est absente
20 * @package SPIP\Texte\Charsets
24 if (!defined('_ECRIRE_INC_VERSION')) return;
28 * Charge en mémoire la liste des caractères d'un charset
30 * Charsets supportes en natif : voir les tables dans ecrire/charsets/
31 * Les autres charsets sont supportes via mbstring()
33 * @param string $charset
35 * Par défaut (AUTO), utilise le charset du site
38 * false si le charset n'est pas décrit dans le répertoire charsets/
40 function load_charset ($charset = 'AUTO') {
41 if ($charset == 'AUTO')
42 $charset = $GLOBALS['meta']['charset'];
43 $charset = trim(strtolower($charset));
44 if (isset($GLOBALS['CHARSET'][$charset]))
47 if ($charset == 'utf-8') {
48 $GLOBALS['CHARSET'][$charset] = array();
53 if ($charset == '') $charset = 'iso-8859-1';
54 else if ($charset == 'windows-1250') $charset = 'cp1250';
55 else if ($charset == 'windows-1251') $charset = 'cp1251';
56 else if ($charset == 'windows-1256') $charset = 'cp1256';
58 if (find_in_path($charset . '.php', 'charsets/', true)) {
61 spip_log("Erreur: pas de fichier de conversion 'charsets/$charset'");
62 $GLOBALS['CHARSET'][$charset] = array();
69 * Verifier qu'on peut utiliser mb_string
72 * true si toutes les fonctions mb nécessaires sont présentes
74 function init_mb_string() {
77 // verifier que tout est present (fonctions mb_string pour php >= 4.0.6)
78 // et que le charset interne est connu de mb_string
80 if (function_exists('mb_internal_encoding')
81 AND function_exists('mb_detect_order')
82 AND function_exists('mb_substr')
83 AND function_exists('mb_strlen')
84 AND function_exists('mb_encode_mimeheader')
85 AND function_exists('mb_encode_numericentity')
86 AND function_exists('mb_decode_numericentity')
87 AND mb_detect_order($GLOBALS['meta']['charset'])
89 mb_internal_encoding('utf-8');
99 * Test le fonctionnement correct d'iconv
101 * Celui-ci coupe sur certaines versions la chaine
102 * quand un caractère n'appartient pas au charset
104 * @link http://php.net/manual/fr/function.iconv.php
107 * true si iconv fonctionne correctement
109 function test_iconv() {
113 if (!function_exists('iconv'))
116 if (utf_32_to_unicode(@iconv
('utf-8', 'utf-32', 'chaine de test')) == 'chaine de test')
122 return ($iconv_ok == 1);
127 * Test de fonctionnement du support UTF-8 dans PCRE
129 * Contournement bug Debian Woody
132 * true si PCRE supporte l'UTF-8 correctement
134 function test_pcre_unicode() {
138 $s = " ".chr(195).chr(169)."t".chr(195).chr(169)." ";
139 if (preg_match(',\W...\W,u', $s)) $pcre_ok = 1;
142 return $pcre_ok == 1;
146 * Renvoie une plage de caractères alphanumeriques unicodes (incomplet...)
148 * Retourne pour une expression rationnelle une plage
149 * de caractères alphanumériques à utiliser entre crochets [$plage]
153 * Servait à inc/ortho passé dans le grenier
155 * Plage de caractères
157 function pcre_lettres_unicode() {
158 static $plage_unicode;
160 if (!$plage_unicode) {
161 if (test_pcre_unicode()) {
162 // cf. http://www.unicode.org/charts/
163 $plage_unicode = '\w' // iso-latin
164 . '\x{100}-\x{24f}' // europeen etendu
165 . '\x{300}-\x{1cff}' // des tas de trucs
169 // fallback a trois sous
170 $plage_unicode = '\w';
173 return $plage_unicode;
178 * Renvoie une plage de caractères de ponctuation unicode de 0x2000 a 0x206F
180 * Retourne pour une expression rationnelle une plage
181 * de caractères de ponctuation à utiliser entre crochets [$plage]
182 * (i.e. de 226-128-128 a 226-129-176)
186 * Servait à inc/ortho passé dans le grenier
188 * Plage de caractères
190 function plage_punct_unicode() {
191 return '\xE2(\x80[\x80-\xBF]|\x81[\x80-\xAF])';
195 * Corriger des caractères non-conformes : 128-159
197 * Cf. charsets/iso-8859-1.php (qu'on recopie ici pour aller plus vite)
198 * On peut passer un charset cible en parametre pour accelerer le passage iso-8859-1 -> autre charset
200 * @param string $texte
201 * Le texte à corriger
202 * @param string $charset
203 * Charset d'origine du texte
204 * Par défaut (AUTO) utilise le charset du site
205 * @param string $charset_cible
206 * Charset de destination (unicode par défaut)
210 function corriger_caracteres_windows($texte, $charset='AUTO', $charset_cible='unicode') {
213 if (is_array($texte)) {
214 return array_map('corriger_caracteres_windows', $texte);
217 if ($charset=='AUTO') $charset = $GLOBALS['meta']['charset'];
218 if ($charset == 'utf-8') {
220 if (strpos($texte,$p)==false)
222 } else if ($charset == 'iso-8859-1') {
227 if (!isset($trans[$charset][$charset_cible])) {
228 $trans[$charset][$charset_cible] = array(
229 $p.chr(128) => "€",
230 $p.chr(129) => ' ', # pas affecte
231 $p.chr(130) => "‚",
232 $p.chr(131) => "ƒ",
233 $p.chr(132) => "„",
234 $p.chr(133) => "…",
235 $p.chr(134) => "†",
236 $p.chr(135) => "‡",
237 $p.chr(136) => "ˆ",
238 $p.chr(137) => "‰",
239 $p.chr(138) => "Š",
240 $p.chr(139) => "‹",
241 $p.chr(140) => "Œ",
242 $p.chr(141) => ' ', # pas affecte
243 $p.chr(142) => "Ž",
244 $p.chr(143) => ' ', # pas affecte
245 $p.chr(144) => ' ', # pas affecte
246 $p.chr(145) => "‘",
247 $p.chr(146) => "’",
248 $p.chr(147) => "“",
249 $p.chr(148) => "”",
250 $p.chr(149) => "•",
251 $p.chr(150) => "–",
252 $p.chr(151) => "—",
253 $p.chr(152) => "˜",
254 $p.chr(153) => "™",
255 $p.chr(154) => "š",
256 $p.chr(155) => "›",
257 $p.chr(156) => "œ",
258 $p.chr(157) => ' ', # pas affecte
259 $p.chr(158) => "ž",
260 $p.chr(159) => "Ÿ",
262 if ($charset_cible!='unicode'){
263 foreach($trans[$charset][$charset_cible] as $k=>$c)
264 $trans[$charset][$charset_cible][$k] = unicode2charset($c, $charset_cible);
268 return @str_replace
(array_keys($trans[$charset][$charset_cible]),
269 array_values($trans[$charset][$charset_cible]),$texte);
275 * Transforme les entités HTML en unicode
277 * Transforme les é en {
279 * @param string $texte
281 * @param bool $secure
282 * true pour *ne pas convertir* les caracteres malins < & etc.
286 function html2unicode($texte, $secure=false) {
287 if (strpos($texte,'&') === false) return $texte;
288 static $trans = array();
291 load_charset('html');
292 foreach ($CHARSET['html'] as $key => $val) {
293 $trans["&$key;"] = $val;
298 return str_replace(array_keys($trans),array_values($trans),$texte);
300 return str_replace(array('&', '"', '<', '>'),array('&', '"', '<', '>'),
301 str_replace(array_keys($trans),array_values($trans),$texte)
307 * Transforme les entités mathématiques (MathML) en unicode
309 * Transforme ∠ en ∠ ainsi que toutes autres entités mathématiques
311 * @param string $texte
316 function mathml2unicode($texte) {
320 load_charset('mathml');
322 foreach ($CHARSET['mathml'] as $key => $val)
323 $trans["&$key;"] = $val;
326 return str_replace(array_keys($trans),array_values($trans),$texte);
331 * Transforme une chaine en entites unicode 
333 * Utilise la librairie mb si elle est présente.
336 * Note: l'argument $forcer est obsolete : il visait a ne pas
337 * convertir les accents iso-8859-1
339 * @param string $texte
341 * @param string $charset
342 * Charset actuel du texte
343 * Par défaut (AUTO), le charset est celui du site.
345 * Texte converti en unicode
347 function charset2unicode($texte, $charset='AUTO' /* $forcer: obsolete*/) {
350 if ($charset == 'AUTO')
351 $charset = $GLOBALS['meta']['charset'];
353 if ($charset == '') $charset = 'iso-8859-1';
354 $charset = strtolower($charset);
359 return utf_8_to_unicode($texte);
362 $texte = corriger_caracteres_windows($texte, 'iso-8859-1');
363 // pas de break; ici, on suit sur default:
366 // mbstring presente ?
367 if (init_mb_string()) {
368 if ($order = mb_detect_order() # mb_string connait-il $charset?
369 AND mb_detect_order($charset)) {
370 $s = mb_convert_encoding($texte, 'utf-8', $charset);
371 if ($s && $s != $texte) return utf_8_to_unicode($s);
373 mb_detect_order($order); # remettre comme precedemment
376 // Sinon, peut-etre connaissons-nous ce charset ?
377 if (!isset($trans[$charset])) {
379 if ($cset = load_charset($charset)
380 AND is_array($CHARSET[$cset]))
381 foreach ($CHARSET[$cset] as $key => $val) {
382 $trans[$charset][chr($key)] = '&#'.$val.';';
385 if (count($trans[$charset]))
386 return str_replace(array_keys($trans[$charset]),array_values($trans[$charset]),$texte);
388 // Sinon demander a iconv (malgre le fait qu'il coupe quand un
389 // caractere n'appartient pas au charset, mais c'est un probleme
390 // surtout en utf-8, gere ci-dessus)
392 $s = iconv($charset, 'utf-32le', $texte);
393 if ($s) return utf_32_to_unicode($s);
396 // Au pire ne rien faire
397 spip_log("erreur charset '$charset' non supporte");
404 * Transforme les entites unicode  dans le charset specifie
406 * Attention on ne transforme pas les entites < € car si elles
407 * ont ete encodees ainsi c'est a dessein
409 * @param string $texte
410 * Texte unicode à transformer
411 * @param string $charset
412 * Charset à appliquer au texte
413 * Par défaut (AUTO), le charset sera celui du site.
415 * Texte transformé dans le charset souhaité
417 function unicode2charset($texte, $charset='AUTO') {
418 static $CHARSET_REVERSE;
419 static $trans = array();
421 if ($charset == 'AUTO')
422 $charset = $GLOBALS['meta']['charset'];
426 return unicode_to_utf_8($texte);
430 $charset = load_charset($charset);
432 if (!is_array($CHARSET_REVERSE[$charset])) {
433 $CHARSET_REVERSE[$charset] = array_flip($GLOBALS['CHARSET'][$charset]);
436 if (!isset($trans[$charset])){
437 $trans[$charset]=array();
438 $t = &$trans[$charset];
439 for($e=128;$e<255;$e++
){
441 if ($s = isset($CHARSET_REVERSE[$charset][$e])){
442 $s = $CHARSET_REVERSE[$charset][$e];
443 $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($s);
444 $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($s);
447 $t['&#'.$e.';'] = $t['�'.$e.';'] = $t['�'.$e.';'] = chr($e);
448 $t['&#x'.$h.';'] = $t['�'.$h.';'] = $t['�'.$h.';'] = chr($e);
452 $texte = str_replace(array_keys($trans[$charset]),array_values($trans[$charset]),$texte);
459 * Importer un texte depuis un charset externe vers le charset du site
461 * Les caracteres non resolus sont transformes en {
463 * @param string $texte
464 * Texte unicode à importer
465 * @param string $charset
466 * Charset d'origine du texte
467 * Par défaut (AUTO), le charset d'origine est celui du site.
469 * Texte transformé dans le charset site
471 function importer_charset($texte, $charset = 'AUTO') {
472 static $trans = array();
473 // on traite le cas le plus frequent iso-8859-1 vers utf directement pour aller plus vite !
474 if (($charset == 'iso-8859-1') && ($GLOBALS['meta']['charset']=='utf-8')){
475 $texte = corriger_caracteres_windows($texte, 'iso-8859-1',$GLOBALS['meta']['charset']);
476 if (init_mb_string()) {
477 if ($order = mb_detect_order() # mb_string connait-il $charset?
478 AND mb_detect_order($charset)) {
479 $s = mb_convert_encoding($texte, 'utf-8', $charset);
481 mb_detect_order($order); # remettre comme precedemment
484 // Sinon, peut-etre connaissons-nous ce charset ?
485 if (!isset($trans[$charset])) {
487 if ($cset = load_charset($charset)
488 AND is_array($CHARSET[$cset]))
489 foreach ($CHARSET[$cset] as $key => $val) {
490 $trans[$charset][chr($key)] = unicode2charset('&#'.$val.';');
493 if (count($trans[$charset]))
494 return str_replace(array_keys($trans[$charset]),array_values($trans[$charset]),$texte);
497 return unicode2charset(charset2unicode($texte, $charset));
502 * Transforme un texte UTF-8 en unicode
504 * Utilise la librairie mb si présente
506 * @param string $source
507 * Texte UTF-8 à transformer
509 * Texte transformé en unicode
511 function utf_8_to_unicode($source) {
513 // mb_string : methode rapide
514 if (init_mb_string()) {
515 $convmap = array(0x7F, 0xFFFFFF, 0x0, 0xFFFFFF);
516 return mb_encode_numericentity($source, $convmap, 'UTF-8');
519 // Sinon methode pas a pas
523 // Cf. php.net, par Ronen. Adapte pour compatibilite < php4
524 if (!is_array($decrement)) {
525 // array used to figure what number to decrement from character order value
526 // according to number of characters used to map unicode to ascii by utf-8
531 // the number of bits to shift each charNum by
545 $len = strlen ($source);
547 while ($pos < $len) {
550 $asciiPos = ord (substr ($source, $pos, 1));
551 if (($asciiPos >= 240) && ($asciiPos <= 255)) {
552 // 4 chars representing one unicode character
553 $thisLetter = substr ($source, $pos, 4);
556 else if (($asciiPos >= 224) && ($asciiPos <= 239)) {
557 // 3 chars representing one unicode character
558 $thisLetter = substr ($source, $pos, 3);
561 else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
562 // 2 chars representing one unicode character
563 $thisLetter = substr ($source, $pos, 2);
567 // 1 char (lower ascii)
568 $thisLetter = substr ($source, $pos, 1);
575 $encodedString .= $char;
576 else { // process the string representing the letter to a unicode entity
577 $thisLen = strlen ($thisLetter);
580 while ($thisPos < $thisLen) {
581 $thisCharOrd = ord (substr ($thisLetter, $thisPos, 1));
583 $charNum = intval ($thisCharOrd - $decrement[$thisLen]);
584 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
586 $charNum = intval ($thisCharOrd - 128);
587 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
591 $encodedLetter = "&#". preg_replace('/^0+/', '', $decimalCode) . ';';
592 $encodedString .= $encodedLetter;
595 return $encodedString;
599 * Transforme un texte UTF-32 en unicode
601 * UTF-32 ne sert plus que si on passe par iconv, c'est-a-dire quand
602 * mb_string est absente ou ne connait pas notre charset.
604 * Mais on l'optimise quand meme par mb_string
605 * => tout ca sera osolete quand on sera surs d'avoir mb_string
607 * @param string $source
608 * Texte UTF-8 à transformer
610 * Texte transformé en unicode
612 function utf_32_to_unicode($source) {
614 // mb_string : methode rapide
615 if (init_mb_string()) {
616 $convmap = array(0x7F, 0xFFFFFF, 0x0, 0xFFFFFF);
617 $source = mb_encode_numericentity($source, $convmap, 'UTF-32LE');
618 return str_replace(chr(0), '', $source);
621 // Sinon methode lente
624 $words = unpack("V*", substr($source, 0, 1024));
625 $source = substr($source, 1024);
626 foreach ($words as $word) {
628 $texte .= chr($word);
629 // ignorer le BOM - http://www.unicode.org/faq/utf_bom.html
630 else if ($word != 65279)
631 $texte .= '&#'.$word.';';
640 * Transforme un numéro unicode en caractère utf-8
642 * Ce bloc provient de php.net
646 * Numéro de l'entité unicode
648 * Caractère utf8 si trouvé, '' sinon
650 function caractere_utf_8($num) {
654 return chr(($num>>6)+
192).chr(($num&63)+
128);
656 return chr(($num>>12)+
224).chr((($num>>6)&63)+
128).chr(($num&63)+
128);
658 return chr($num>>18+
240).chr((($num>>12)&63)+
128).chr(($num>>6)&63+
128). chr($num&63+
128);
663 * Convertit un texte unicode en utf-8
665 * @param string $texte
670 function unicode_to_utf_8($texte) {
672 // 1. Entites € et suivantes
674 if (preg_match_all(',�*([1-9][0-9][0-9]+);,S',
675 $texte, $regs, PREG_SET_ORDER
))
676 foreach ($regs as $reg) {
677 if ($reg[1]>127 AND !isset($vu[$reg[0]]))
678 $vu[$reg[0]] = caractere_utf_8($reg[1]);
680 //$texte = str_replace(array_keys($vu), array_values($vu), $texte);
682 // 2. Entites > ÿ
684 if (preg_match_all(',�*([1-9a-f][0-9a-f][0-9a-f]+);,iS',
685 $texte, $regs, PREG_SET_ORDER
))
686 foreach ($regs as $reg) {
687 if (!isset($vu[$reg[0]]))
688 $vu[$reg[0]] = caractere_utf_8(hexdec($reg[1]));
690 return str_replace(array_keys($vu), array_values($vu), $texte);
695 * Convertit les unicode Ĉ en javascript \u0108
697 * @param string $texte
702 function unicode_to_javascript($texte) {
704 while (preg_match(',�*([0-9]+);,S', $texte, $regs) AND !isset($vu[$regs[1]])) {
707 $s = '\u'.sprintf("%04x", $num);
708 $texte = str_replace($regs[0], $s, $texte);
714 * Convertit les %uxxxx (envoyés par javascript) en &#yyy unicode
716 * @param string $texte
721 function javascript_to_unicode ($texte) {
722 while (preg_match(",%u([0-9A-F][0-9A-F][0-9A-F][0-9A-F]),", $texte, $regs))
723 $texte = str_replace($regs[0],"&#".hexdec($regs[1]).";", $texte);
728 * Convertit les %E9 (envoyés par le browser) en chaîne du charset du site (binaire)
730 * @param string $texte
735 function javascript_to_binary ($texte) {
736 while (preg_match(",%([0-9A-F][0-9A-F]),", $texte, $regs))
737 $texte = str_replace($regs[0],chr(hexdec($regs[1])), $texte);
742 // http://doc.spip.org/@translitteration_rapide
743 function translitteration_rapide($texte, $charset='AUTO', $complexe='') {
745 if ($charset == 'AUTO')
746 $charset = $GLOBALS['meta']['charset'];
750 $table_translit ='translit'.$complexe;
752 // 2. Translitterer grace a la table predefinie
753 if (!$trans[$complexe]) {
755 load_charset($table_translit);
756 foreach ($CHARSET[$table_translit] as $key => $val)
757 $trans[$complexe][caractere_utf_8($key)] = $val;
760 return str_replace(array_keys($trans[$complexe]),array_values($trans[$complexe]),$texte);
764 // Translitteration charset => ascii (pour l'indexation)
765 // Attention les caracteres non reconnus sont renvoyes en utf-8
767 // http://doc.spip.org/@translitteration
768 function translitteration($texte, $charset='AUTO', $complexe='') {
769 // 0. Supprimer les caracteres illegaux
770 include_spip('inc/filtres');
771 $texte = corriger_caracteres($texte);
773 // 1. Passer le charset et les é en utf-8
774 $texte = unicode_to_utf_8(html2unicode(charset2unicode($texte, $charset, true)));
776 return translitteration_rapide($texte,$charset,$complexe);
779 // à est retourne sous la forme "a`" et pas "a"
780 // mais si $chiffre=true, on retourne "a8" (vietnamien)
781 // http://doc.spip.org/@translitteration_complexe
782 function translitteration_complexe($texte, $chiffres=false) {
783 $texte = translitteration($texte,'AUTO','complexe');
786 $texte = preg_replace("/[aeiuoyd]['`?~.^+(-]{1,2}/eS",
787 "translitteration_chiffree('\\0')", $texte);
792 // http://doc.spip.org/@translitteration_chiffree
793 function translitteration_chiffree($car) {
794 return strtr($car, "'`?~.^+(-", "123456789");
799 * Reconnaitre le BOM utf-8 (0xEFBBBF)
801 * @param string $texte
802 * Texte dont on vérifie la présence du BOM
806 function bom_utf8($texte) {
807 return (substr($texte, 0,3) == chr(0xEF).chr(0xBB).chr(0xBF));
811 * Vérifie qu'une chaîne est en utf-8 valide
813 * Note: preg_replace permet de contourner un "stack overflow" sur PCRE
815 * @link http://us2.php.net/manual/fr/function.mb-detect-encoding.php#50087
816 * @link http://w3.org/International/questions/qa-forms-utf-8.html
818 * @param string $string
819 * Texte dont on vérifie qu'il est de l'utf-8
821 * true si c'est le cas
823 function is_utf8($string) {
826 ',[\x09\x0A\x0D\x20-\x7E]' # ASCII
827 . '|[\xC2-\xDF][\x80-\xBF]' # non-overlong 2-byte
828 . '|\xE0[\xA0-\xBF][\x80-\xBF]' # excluding overlongs
829 . '|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}' # straight 3-byte
830 . '|\xED[\x80-\x9F][\x80-\xBF]' # excluding surrogates
831 . '|\xF0[\x90-\xBF][\x80-\xBF]{2}' # planes 1-3
832 . '|[\xF1-\xF3][\x80-\xBF]{3}' # planes 4-15
833 . '|\xF4[\x80-\x8F][\x80-\xBF]{2}' # plane 16
839 * Vérifie qu'une chaîne est en ascii valide
841 * @param string $string
842 * Texte dont on vérifie qu'il est de l'ascii
844 * true si c'est le cas
846 function is_ascii($string) {
849 ',[\x09\x0A\x0D\x20-\x7E],sS',
853 // Transcode une page (attrapee sur le web, ou un squelette) en essayant
854 // par tous les moyens de deviner son charset (y compris headers HTTP)
855 // http://doc.spip.org/@transcoder_page
856 function transcoder_page($texte, $headers='') {
858 // Si tout est < 128 pas la peine d'aller plus loin
859 if (is_ascii($texte)) {
860 #spip_log('charset: ascii');
864 // Reconnaitre le BOM utf-8 (0xEFBBBF)
865 if (bom_utf8($texte)) {
867 $texte = substr($texte,3);
870 // charset precise par le contenu (xml)
872 ',<[?]xml[^>]*encoding[^>]*=[^>]*([-_a-z0-9]+?),UimsS', $texte, $regs))
873 $charset = trim(strtolower($regs[1]));
874 // charset precise par le contenu (html)
876 ',<(meta|html|body)[^>]*charset[^>]*=[^>]*([-_a-z0-9]+?),UimsS',
878 # eviter #CHARSET des squelettes
879 AND (($tmp = trim(strtolower($regs[2]))) != 'charset'))
881 // charset de la reponse http
882 else if (preg_match(',charset=([-_a-z0-9]+),i', $headers, $regs))
883 $charset = trim(strtolower($regs[1]));
885 // normaliser les noms du shif-jis japonais
886 if (preg_match(',^(x|shift)[_-]s?jis$,i', $charset))
887 $charset = 'shift-jis';
890 spip_log("charset: $charset");
896 $charset = 'iso-8859-1';
897 spip_log("charset probable: $charset");
900 return importer_charset($texte, $charset);
905 // Gerer les outils mb_string
909 * Coupe un texte selon substr()
911 * Coupe une chaîne en utilisant les outils mb* lorsque le site est en utf8
913 * @link http://fr.php.net/manual/fr/function.mb-substr.php
914 * @link http://www.php.net/manual/fr/function.substr.php
916 * @param string $c Le texte
917 * @param int $start Début
918 * @param null|int $length Longueur ou fin
922 function spip_substr($c, $start=0, $length = NULL) {
923 // Si ce n'est pas utf-8, utiliser substr
924 if ($GLOBALS['meta']['charset'] != 'utf-8') {
926 return substr($c, $start, $length);
931 // Si utf-8, voir si on dispose de mb_string
932 if (init_mb_string()) {
934 return mb_substr($c, $start, $length);
936 return mb_substr($c, $start);
939 // Version manuelle (cf. ci-dessous)
940 return spip_substr_manuelle($c, $start, $length);
945 * Coupe un texte comme mb_substr()
947 * Version manuelle de substr utf8, pour php vieux et/ou mal installe
949 * @link http://fr.php.net/manual/fr/function.mb-substr.php
950 * @used-by spip_substr()
952 * @param string $c Le texte
953 * @param int $start Début
954 * @param null|int $length Longueur ou fin
958 function spip_substr_manuelle($c, $start, $length = NULL) {
964 // S'il y a un demarrage, on se positionne
966 $c = substr($c, strlen(spip_substr_manuelle($c, 0, $start)));
968 return spip_substr_manuelle($c, spip_strlen($c)+
$start, $length);
974 // on prend n fois la longueur desiree, pour etre surs d'avoir tout
975 // (un caractere utf-8 prenant au maximum n bytes)
976 $n = 0; while (preg_match(',[\x80-\xBF]{'.(++
$n).'},', $c));
977 $c = substr($c, 0, $n*$length);
978 // puis, tant qu'on est trop long, on coupe...
979 while (($l = spip_strlen($c)) > $length)
980 $c = substr($c, 0, $length - $l);
985 return spip_substr_manuelle($c, 0, spip_strlen($c)+
$length);
989 * Rend majuscule le premier caractère d'une chaîne utf-8
991 * Version utf-8 d'ucfirst
994 * La chaîne à transformer
996 * La chaîne avec une majuscule sur le premier mot
998 function spip_ucfirst($c){
999 // Si ce n'est pas utf-8, utiliser ucfirst
1000 if ($GLOBALS['meta']['charset'] != 'utf-8')
1002 // Si on n'a pas mb_* on utilise ucfirst
1003 if (!init_mb_string())
1006 $lettre1 = mb_strtoupper(spip_substr($c,0,1));
1007 return $lettre1.spip_substr($c,1);
1011 * Retourne la longueur d'une chaîne utf-8
1013 * Version utf-8 de strlen
1016 * La chaîne à compter
1018 * Longueur de la chaîne
1020 function spip_strlen($c) {
1021 // On transforme les sauts de ligne pour ne pas compter deux caractères
1022 $c = str_replace("\r\n", "\n", $c);
1024 // Si ce n'est pas utf-8, utiliser strlen
1025 if ($GLOBALS['meta']['charset'] != 'utf-8')
1028 // Sinon, utiliser mb_strlen() si disponible
1029 if (init_mb_string())
1030 return mb_strlen($c);
1032 // Methode manuelle : on supprime les bytes 10......,
1033 // on compte donc les ascii (0.......) et les demarrages
1034 // de caracteres utf-8 (11......)
1035 return strlen(preg_replace(',[\x80-\xBF],S', '', $c));
1039 $GLOBALS['CHARSET'] = Array();
1041 // noter a l'occasion dans la meta pcre_u notre capacite a utiliser le flag /u
1042 // dans les preg_replace pour ne pas casser certaines lettres accentuees :
1043 // en utf-8 chr(195).chr(160) = a` alors qu'en iso-latin chr(160) = nbsp
1044 if (!isset($GLOBALS['meta']['pcre_u'])
1045 OR (isset($_GET['var_mode']) AND !isset($_GET['var_profile']))) {
1046 include_spip('inc/meta');
1047 ecrire_meta('pcre_u',
1048 $u = ($GLOBALS['meta']['charset'] == 'utf-8'
1049 AND test_pcre_unicode())