6a9fb5183626850494484e1723048880ab24c440
4 * Transforme une chaine encodée en UTF-8, et la convertit
5 * en entitiées unicode &#xxx; pour que ça s'affiche correctement
6 * dans les navigateurs, sans forcément tenir compte du meta
7 * content-type charset...
8 * @param String $source la chaine en UTF-8
9 * @return String les entitées
11 * @see http://www.php.net/utf8_decode
13 function utf8entities($source)
15 // array used to figure what number to decrement from character order value
16 // according to number of characters used to map unicode to ascii by utf-8
22 // the number of bits to shift each charNum by
35 $len = strlen($source);
39 $charPos = $source{$pos};
40 $asciiPos = ord($charPos);
43 $encodedString .= $charPos;
49 if (($asciiPos >= 240) && ($asciiPos <= 255)) // 4 chars representing one unicode character
51 else if (($asciiPos >= 224) && ($asciiPos <= 239)) // 3 chars representing one unicode character
53 else if (($asciiPos >= 192) && ($asciiPos <= 223)) // 2 chars representing one unicode character
55 else // 1 char (lower ascii)
57 $thisLetter = substr($source, $pos, $i);
60 // process the string representing the letter to a unicode entity
61 $thisLen = strlen($thisLetter);
64 while ($thisPos < $thisLen)
66 $thisCharOrd = ord(substr($thisLetter, $thisPos, 1));
69 $charNum = intval($thisCharOrd - $decrement[$thisLen]);
70 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
74 $charNum = intval($thisCharOrd - 128);
75 $decimalCode +
= ($charNum << $shift[$thisLen][$thisPos]);
81 $encodedLetter = '&#'. str_pad($decimalCode, ($thisLen==1)?
3:5, '0', STR_PAD_LEFT
).';';
82 $encodedString .= $encodedLetter;
85 return $encodedString;