2011-07-09 2 views
1

Le problème: j'ai trouvé un code php (jeu du pendu) et je veux qu'il fonctionne en allemand mais il sort A BCF au lieu de ces lettres: Ü Ä ÖProblème d'encodage php et utf-8 avec les lettres allemandes

Comment puis-je le réparer .. et utiliser uniquement l'encodage utf-8 ??

ici est tout le code:

<?php 
$Category = "Web Programming"; 

$list = "VERSCHLÜSSELUNG"; 

$alpha = "AÜÄÖÇBCDEFGHIJKLMNOPQRSTUYVWXYZ"; 

$additional_letters = " -.,;!?%&"; 
mb_internal_encoding("UTF-8"); 
$len_alpha = mb_strlen($alpha); 
if(isset($_GET["n"])) $n=$_GET["n"]; 
if(isset($_GET["letters"])) $letters=$_GET["letters"]; 
if(!isset($letters)) $letters=""; 
if(isset($PHP_SELF)) $self=$PHP_SELF; 
else $self=$_SERVER["PHP_SELF"]; 

$links=""; 
$max=6;  # maximum number of wrong 
# error_reporting(0); 

$words = explode("\n",$list); 
srand ((double)microtime()*1000000); 
$all_letters=$letters.$additional_letters; 
$wrong = 0; 

if (!isset($n)) { $n = rand(1,count($words)) - 1; } 
$word_line=""; 
$word = trim($words[$n]); 
$done = 1; 


for ($x=0; $x < mb_strlen($word); $x++) 
{ 
    if (strstr($all_letters, $word[$x])) 
    { 
    if ($word[$x]==" ") $word_line.="&nbsp; "; else $word_line.=$word[$x]; 
    } 
    else { $word_line.="_<font size=1>&nbsp;</font>"; $done = 0; } 
} 


if (!$done) 
{ 
    for ($c=0; $c<$len_alpha; $c++) 
    { 
    if (mb_strstr($letters, $alpha[$c])) 

    { 
    if (mb_strstr($words[$n], $alpha[$c])) 
{$links .= "\n<B>$alpha[$c]</B> "; 
} 

     else { $links .= "\n<FONT color=\"red\">$alpha[$c] </font>"; $wrong++; 
} 
    } 
    else 
    { 
$links .= "\n<A HREF=\"$self?letters=$alpha[$c]$letters&n=$n\">$alpha[$c]</A> "; 
} 
    } 
$nwrong=$wrong; if ($nwrong>6) $nwrong=6; 
    echo "\n<p><BR>\n<IMG SRC=\"hangman_$nwrong.gif\" ALIGN=\"MIDDLE\" BORDER=0 WIDTH=110 HEIGHT=185 ALT=\"Wrong: $wrong out of $max\">\n"; 
    if ($wrong >= $max) 
    { 
    $n++; 
    if ($n>(count($words)-1)) $n=0; 
    echo "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n"; 
    echo "<p><BR><FONT color=\"red\"><BIG>You Lost!</BIG></FONT><BR><BR>"; 
    if (strstr($word, " ")) $term="fraz?"; else $term="?odis"; 
    echo "The word was \"<B>$word</B>\"<BR><BR>\n"; 
    echo "<A HREF=$self?n=$n>Play Again... </A>\n\n"; 
    } 
    else 
    { 
    echo " &nbsp; Remaining guesses: <B>".($max-$wrong)."</B><BR>\n"; 
    echo "<H1><font size=5>\n$word_line</font></H1>\n"; 
    echo "<P><BR>Please choose a letter: <BR><BR>\n"; 
     echo "<font size=3> $links \n</font>"; 
    } 
} 

    else 
    { 
     $n++; # get next word 
     if ($n>(count($words)-1)) $n=0; 
     echo "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n"; 
     echo "<P><BR><BR><B><font color=red size=2>You Won</font></B><BR><BR><BR>\n"; 
     echo "<A HREF=$self?n=$n>Play Again... </A>\n\n"; 
    } 
    ?> 
+0

Où se trouve l'en-tête html? – CodesInChaos

+0

@CodeInChaos .. l'en-tête html est cochée, c'est ok .. – marko

+0

Et quel est le type de doc dans l'en-tête http? Et votre fichier est-il encodé en utf8? – CodesInChaos

Répondre

0

vous devez spécifier UTF8 dans votre code HTML:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
+0

merci, mais je l'ai fait! – marko

4

Ceci est faux lorsque l'on travaille avec encodages multi-octets:

$alpha[$c] 

Cela va adresser un octet en $alpha, pas un caractère. Utilisez mb_substr à la place:

mb_substr($alpha, $c, 1); 
+0

@ Emil Vikström je pense .. c'est le problème, comment puis-je utiliser mb_substr – marko

+0

marko, j'ai mis à jour ma réponse avec un exemple. –

+0

désolé mais je ne peux pas faire les modifications sur ce code ..> (mb_strstr ($ lettres, $ alpha [$ c])) { si (mb_strstr ($ mots [$ n], $ alpha [$ c ])) {$ links. = "\ N $ alpha [$ c]"; } else {$ links. = "\ N $ alpha [$ c]"; $ faux ++; } } sinon { $ links. = "\ N $alpha[$c]"; } – marko

1

La plupart des navigateurs ces jours-ci ignorent les balises meta-type de contenu. Il n'y a pas besoin de les utiliser. Vous devez faire une tête de page réelle en utilisant php avant toute sortie:

header("Content-type: text/html; charset=UTF-8"); 
1

Pour les mots allemands, nous devons utiliser l'encodage ISO-8859 qui est compatible avec les mots européens. Cette solution a fonctionné pour moi avant d'encoder JSON.

$finalString = htmlentities($GermanWord,ENT_QUOTES | ENT_IGNORE | ENT_SUBSTITUTE | ENT_DISALLOWED | ENT_HTML401 | ENT_XML1 | ENT_XHTML | ENT_HTML5, "ISO-8859-1"); 

Vous pouvez également encoder la chaîne avec UTF-8 pour html est ISO-8859-1 ne fonctionne pas.

référez ce document http://php.net/manual/en/function.htmlentities.php pour modifier les paramètres de codage.

0

Utilisez ceci.

mb_strtolower(mb_convert_encoding($_POST['entered_key'], 'UTF-8', 'UTF-8'), 'UTF-8')