IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP Discussion :

UTF-8 to html (unicode)


Sujet :

ASP

  1. #1
    Membre confirmé Avatar de d-Rek
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    438
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2007
    Messages : 438
    Points : 455
    Points
    455
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  2. #2
    Membre confirmé Avatar de d-Rek
    Homme Profil pro
    Développeur Web
    Inscrit en
    Janvier 2007
    Messages
    438
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Janvier 2007
    Messages : 438
    Points : 455
    Points
    455
    Par défaut
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 5
    Dernier message: 04/03/2011, 15h04
  2. [XHTML] Problème de Unicode Byte-Order Mark (BOM) in UTF-8 (?)
    Par gb-ch dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 13/02/2007, 02h01
  3. [XSLT] XML+XSL=HTML avec charset UTF-16
    Par Steph4fun dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 02/10/2006, 07h54
  4. conversion iso-8859-1 => UTF-8 (unicode)
    Par pierru666 dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 30/05/2006, 22h17
  5. [unicode] mauvais rendu du code utf.
    Par PyBio dans le forum Général Python
    Réponses: 4
    Dernier message: 04/11/2005, 21h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo