2011-07-09 73 views
1

問題:我找到了一個php代碼(hang子手遊戲),我希望它能夠使用德語,但它輸出的是A BCF而不是這些字母:Ü Ä使用德文字母的php和utf-8編碼問題

我該如何解決它..並只使用utf-8編碼?

這裏是整個代碼:

<?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

html標頭在哪裏? – CodesInChaos

+0

@CodeInChaos .. html標題被選中,沒關係.. – marko

+0

什麼是http頭文件類型?你的文件是utf8編碼的嗎? – CodesInChaos

回答

0

你需要在你的HTML指定UTF8:

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

謝謝,但我做到了! – marko

4

這多字節編碼的工作時,是錯誤的:

$alpha[$c] 

這將處理$alpha中的一個字節,而不是一個字符。改爲使用mb_substr

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

@ EmilVikström我認爲..這是問題,我如何使用mb_substr – marko

+0

marko,我用一個例子更新了我的答案。 (mb_strstr($ letters,$ alpha [$ c])) { if(mb_strstr($ words [$ n],$ alpha [$ c, –

+0

)對不起,但我無法對此代碼進行更改。 ])) {$ links。=「\ n $ alpha [$ c]」; } else {$ links。=「\ n $ alpha [$ c]」; $錯++; } } else { $ links。=「\ n $alpha[$c]」; } – marko

1

現在大多數瀏覽器忽略元內容類型標記。沒有必要使用它們。你需要的任何輸出之前做的使用PHP的實際網頁標題:

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

德國的話,我們需要使用ISO-8859編碼,這是歐洲的話兼容。 此解決方案在編碼JSON之前爲我工作。

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

您也可以編碼字符串UTF-8對HTML是ISO-8859-1不起作用。

參考本文檔http://php.net/manual/en/function.htmlentities.php更改編碼參數。

0

使用此。

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