Précédent   Forum des professionnels en informatique > Webmasters - Développement Web > Langages serveur > ASP
ASP Forum sur la programmation ASP. Avant de poster : Cours ASP, FAQ ASP
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 01/09/2011, 09h36   #1
Membre éprouvé
 
Avatar de d-Rek
 
Développeur Web
Inscription : janvier 2007
Messages : 438
Détails du profil
Informations personnelles :
Localisation : France, Nord (Nord Pas de Calais)

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2007
Messages : 438
Points : 400
Points : 400
Par défaut UTF-8 to html (unicode)

L'idée est de sa baser sur un code fonctionnel en PHP qui converti les caractères UTF-8 par leur équivalent HTML.
Exemple : Ł sera converti en Voici le code PHP que j'ai traduis :
Code PHP :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?
function utf8_to_html($data){
	return preg_replace("/([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)/e", '_utf8_to_html("\\1")', $data);
}
 
function _utf8_to_html($data){
	$ret = 0;
	foreach((str_split(strrev(chr((ord($data{0}) % 252 % 248 % 240 % 224 % 192) + 128) . substr($data, 1)))) as $k => $v)
		$ret += (ord($v) % 128) * pow(64, $k);
	return "&#$ret;";
}
 
echo utf8_to_html(file_get_contents('mon_fichier_utf-8.txt'));
?>

Voici ma traduction :
Code ASP :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<%
Response.CodePage="28591"
Response.Charset="ISO-8859-1"
 
function str_split(str)
	dim tab()
	redim tab(len(str))
	for i=0 to len(str)
		tab(i)=mid(str, i+1, 1)
	next
	str_split=tab
end function
 
function strrev(strOrig)
	dim strReverse
	For x = Len(strOrig) To 1 Step -1
		charA = Mid(strOrig, x, 1)
		strReverse = strReverse & charA
	Next
	strrev=strReverse
end function
 
function sub_utf8_to_html(data)
	dim v
	ret=0
	for k=0 to Ubound(str_split(strrev(Chr((asc(mid(data, 1, 1)) mod 252 mod 248 mod 240 mod 224 mod 192) + 128) & mid(data, 2))))-1
		v=(str_split(strrev(Chr((asc(mid(data, 1, 1)) mod 252 mod 248 mod 240 mod 224 mod 192) + 128) & mid(data, 2)))(k))
		ret=ret+((asc(v) mod 128) * (64^k))
	next
	sub_utf8_to_html="&#"&ret&";"
end function
 
function utf8_to_html(data)
	Set objRegExp=New RegExp
	objRegExp.Global=True
	objRegExp.IgnoreCase=true
	objRegExp.Pattern="([\\xC0-\\xF7]{1,1}[\\x80-\\xBF]+)"
	objRegExp.MultiLine=true
	newstr=data
	Set Matches = objRegExp.Execute(newstr)
	if Matches.Count>0 then
		For Match=0 to Matches.count-1
			echo "Found : "&Matches(Match)&"<br>"
			newstr=replace(newstr, Matches(Match), sub_utf8_to_html(Matches(Match)))
		next
	end if
	utf8_to_html=newstr
	set objRegExp = nothing
end function
 
sub echo(s)
	response.write s
end sub
 
chaine = ""
Set ASPTear = Server.CreateObject("SOFTWING.ASPTear")
chaine = ASPTear.Retrieve(Cstr("http://www.monsite.com/mon_fichier_utf-8.txt"), 2, "", "", "")
Set ASPTear=Nothing
 
echo "Source : "&chaine&VbCrLf&"<br>"&VbCrLf&"<br>"&VbCrLf
echo "Convert : "&utf8_to_html(chaine)
%>

Le trace dans la fonction ASP utf8_to_html montre que ce qui est matché, est en fait les caractères suivants le caractère UTF-8 à convertir.
Pourtant les regexp ne sont-elles pas universelles ?

Si quelqu'un à une idée..

A noter que les scripts sont utilisés sur des documents encodés en ANSI et non UTF-8 sinon ça n'a aucun intérêt.
Par contre, mon_fichier_utf-8.txt est encodé en UTF-8 sans BOM.
__________________
Mon blog
d-Rek est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 01/09/2011, 12h35   #2
Membre éprouvé
 
Avatar de d-Rek
 
Développeur Web
Inscription : janvier 2007
Messages : 438
Détails du profil
Informations personnelles :
Localisation : France, Nord (Nord Pas de Calais)

Informations professionnelles :
Activité : Développeur Web

Informations forums :
Inscription : janvier 2007
Messages : 438
Points : 400
Points : 400
J'ai traduit une autre fonction PHP qui faisait pareil..
Et cette fois qui fonctionne
Vous pouvez poster de l'UTF-8 depuis une page tierce, le script va convertir tout ça comme il faut.

Je partage, Enjoy!

Code ASP :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<%
Response.Charset="ISO-8859-1"
 
sub echo(s)
	response.write s
end sub
 
function numeric_entify_utf8(utf8_string)
	out=""
	ns=len(utf8_string)
	for nn=0 to ns-1
		ch=mid(utf8_string, nn+1, 1)
		ii=asc(ch)
		if ii<128 then '1 7 0bbbbbbb (127)
			out=out&ch
		elseif(int(ii/32)=6) then '2 11 110bbbbb 10bbbbbb (2047)
			b1=(ii and 31)
			nn=nn+1
			ch=mid(utf8_string, nn+1, 1)
			ii=asc(ch)
			b2=(ii and 63)
			ii=(b1 * 64) + b2
			ent="&#"&ii&";"
			out=out&ent
		elseif(int(ii/16)=14) then '3 16 1110bbbb 10bbbbbb 10bbbbbb
			b1=(ii and 31)
			nn=nn+1
			ch=mid(utf8_string, nn+1, 1)
			ii=asc(ch)
			b2=(ii and 63)
			nn=nn+1
			ch=mid(utf8_string, nn+1, 1)
			ii=asc(ch)
			b3=(ii and 63)
			ii=(((b1 * 64) + b2) * 64) + b3
			ent="&#"&ii&";"
			out=out&ent
		elseif(int(ii/8)=30) then '4 21 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
			b1=(ii and 31)
			nn=nn+1
			ch=mid(utf8_string, nn+1, 1)
			ii=asc(ch)
			b2=(ii and 63)
			nn=nn+1
			ch=mid(utf8_string, nn+1, 1)
			ii=asc(ch)
			b3=(ii and 63)
			nn=nn+1
			ch=mid(utf8_string, nn+1, 1)
			ii=asc(ch)
			b4=(ii and 63)
			ii=(((((b1 * 64) + b2) * 64) + b3) * 64) + b4
			ent="&#"&ii&";"
			out=out&ent
		end if
	next
	numeric_entify_utf8=out
end function
 
if Request.Form("str")<>"" then
	chaine = replace(replace(Request("str"), "<", "&lt;"), ">", "&gt;")
else
	Set ASPTear = Server.CreateObject("SOFTWING.ASPTear")
	chaine = ASPTear.Retrieve(Cstr("http://www.monsite.com/mon_fichier_utf-8.txt"), 2, "", "", "")
	Set ASPTear=Nothing
end if
 
echo numeric_entify_utf8(chaine)
%>

Code source original en PHP
Traduction en ASP par moi même
__________________
Mon blog
d-Rek est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 18h21.


 
 
 
 
Partenaires

Hébergement Web