Convertir du XML en HTML – Fonction PHP

Voici une petite fonction utile pour convertir tous les symboles spéciaux d’une chaine XML en HTML. Si vous désirez ensuite traduire cette chaine en UTF, il suffira d’utiliser la fonction htmlspecialchars_decode () native pour PHP5.

Fonction de conversion du XML en HTML


function xmltohtml($string) {

$patterns = array();
$replacements = array();


$patterns[0] = '/"/';$replacements[0] = '"';
$patterns[1] = '/&/';$replacements[1] = '&';
$patterns[2] = '/</';$replacements[2] = '<';
$patterns[3] = '/>/';$replacements[3] = '>';
$patterns[4] = '/ /';$replacements[4] = ' ';
$patterns[5] = '/¡/';$replacements[5] = '¡';
$patterns[6] = '/¢/';$replacements[6] = '¢';
$patterns[7] = '/£/';$replacements[7] = '£';
$patterns[8] = '/¤/';$replacements[8] = '¤';
$patterns[9] = '/¥/';$replacements[9] = '¥';
$patterns[10] = '/¦/';$replacements[10] = '¦';
$patterns[11] = '/§/';$replacements[11] = '§';
$patterns[12] = '/¨/';$replacements[12] = '¨';
$patterns[13] = '/©/';$replacements[13] = '©';
$patterns[14] = '/ª/';$replacements[14] = 'ª';
$patterns[15] = '/«/';$replacements[15] = '«';
$patterns[16] = '/¬/';$replacements[16] = '¬';
$patterns[17] = '/­/';$replacements[17] = '­';
$patterns[18] = '/®/';$replacements[18] = '®';
$patterns[19] = '/¯/';$replacements[19] = '¯';
$patterns[20] = '/°/';$replacements[20] = '°';
$patterns[21] = '/±/';$replacements[21] = '±';
$patterns[22] = '/²/';$replacements[22] = '²';
$patterns[23] = '/³/';$replacements[23] = '³';
$patterns[24] = '/´/';$replacements[24] = '´';
$patterns[25] = '/µ/';$replacements[25] = 'µ';
$patterns[26] = '/¶/';$replacements[26] = '¶';
$patterns[27] = '/·/';$replacements[27] = '·';
$patterns[28] = '/¸/';$replacements[28] = '¸';
$patterns[29] = '/¹/';$replacements[29] = '¹';
$patterns[30] = '/º/';$replacements[30] = 'º';
$patterns[31] = '/»/';$replacements[31] = '»';
$patterns[32] = '/¼/';$replacements[32] = '¼';
$patterns[33] = '/½/';$replacements[33] = '½';
$patterns[34] = '/¾/';$replacements[34] = '¾';
$patterns[35] = '/¿/';$replacements[35] = '¿';
$patterns[36] = '/À/';$replacements[36] = 'À';
$patterns[37] = '/Á/';$replacements[37] = 'Á';
$patterns[38] = '/Â/';$replacements[38] = 'Â';
$patterns[39] = '/Ã/';$replacements[39] = 'Ã';
$patterns[40] = '/Ä/';$replacements[40] = 'Ä';
$patterns[41] = '/Å/';$replacements[41] = 'Å';
$patterns[42] = '/Æ/';$replacements[42] = 'Æ';
$patterns[43] = '/Ç/';$replacements[43] = 'Ç';
$patterns[44] = '/È/';$replacements[44] = 'È';
$patterns[45] = '/É/';$replacements[45] = 'É';
$patterns[46] = '/Ê/';$replacements[46] = 'Ê';
$patterns[47] = '/Ë/';$replacements[47] = 'Ë';
$patterns[48] = '/Ì/';$replacements[48] = 'Ì';
$patterns[49] = '/Í/';$replacements[49] = 'Í';
$patterns[50] = '/Î/';$replacements[50] = 'Î';
$patterns[51] = '/Ï/';$replacements[51] = 'Ï';
$patterns[52] = '/Ð/';$replacements[52] = 'Ð';
$patterns[53] = '/Ñ/';$replacements[53] = 'Ñ';
$patterns[54] = '/Ò/';$replacements[54] = 'Ò';
$patterns[55] = '/Ó/';$replacements[55] = 'Ó';
$patterns[56] = '/Ô/';$replacements[56] = 'Ô';
$patterns[57] = '/Õ/';$replacements[57] = 'Õ';
$patterns[58] = '/Ö/';$replacements[58] = 'Ö';
$patterns[59] = '/×/';$replacements[59] = '×';
$patterns[60] = '/Ø/';$replacements[60] = 'Ø';
$patterns[61] = '/Ù/';$replacements[61] = 'Ù';
$patterns[62] = '/Ú/';$replacements[62] = 'Ú';
$patterns[63] = '/Û/';$replacements[63] = 'Û';
$patterns[64] = '/Ü/';$replacements[64] = 'Ü';
$patterns[65] = '/Ý/';$replacements[65] = 'Ý';
$patterns[66] = '/Þ/';$replacements[66] = 'Þ';
$patterns[67] = '/ß/';$replacements[67] = 'ß';
$patterns[68] = '/à/';$replacements[68] = 'à';
$patterns[69] = '/á/';$replacements[69] = 'á';
$patterns[70] = '/â/';$replacements[70] = 'â';
$patterns[71] = '/ã/';$replacements[71] = 'ã';
$patterns[72] = '/ä/';$replacements[72] = 'ä';
$patterns[73] = '/å/';$replacements[73] = 'å';
$patterns[74] = '/æ/';$replacements[74] = 'æ';
$patterns[75] = '/ç/';$replacements[75] = 'ç';
$patterns[76] = '/è/';$replacements[76] = 'è';
$patterns[77] = '/é/';$replacements[77] = 'é';
$patterns[78] = '/ê/';$replacements[78] = 'ê';
$patterns[79] = '/ë/';$replacements[79] = 'ë';
$patterns[80] = '/ì/';$replacements[80] = 'ì';
$patterns[81] = '/í/';$replacements[81] = 'í';
$patterns[82] = '/î/';$replacements[82] = 'î';
$patterns[83] = '/ï/';$replacements[83] = 'ï';
$patterns[84] = '/ð/';$replacements[84] = 'ð';
$patterns[85] = '/ñ/';$replacements[85] = 'ñ';
$patterns[86] = '/ò/';$replacements[86] = 'ò';
$patterns[87] = '/ó/';$replacements[87] = 'ó';
$patterns[88] = '/ô/';$replacements[88] = 'ô';
$patterns[89] = '/õ/';$replacements[89] = 'õ';
$patterns[90] = '/ö/';$replacements[90] = 'ö';
$patterns[91] = '/÷/';$replacements[91] = '÷';
$patterns[92] = '/ø/';$replacements[92] = 'ø';
$patterns[93] = '/ù/';$replacements[93] = 'ù';
$patterns[94] = '/ú/';$replacements[94] = 'ú';
$patterns[95] = '/û/';$replacements[95] = 'û';
$patterns[96] = '/ü/';$replacements[96] = 'ü';
$patterns[97] = '/ý/';$replacements[97] = 'ý';
$patterns[98] = '/þ/';$replacements[98] = 'þ';
$patterns[99] = '/ÿ/';$replacements[99] = 'ÿ';
$patterns[100] = '/€/';$replacements[100] = '€';
return preg_replace($patterns, $replacements, $string);
}

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *