[英]php and utf-8 encoding problem with german letters
问题:我找到了一个 php 代码(刽子手游戏),我希望它在德语上工作,但它输出 A � ���B C F 而不是这些字母:Ü Ä Ö
我该如何修复它..并仅使用 utf-8 编码?
这是整个代码:
<?php
$Category = "Web Programming";
$list = "VERSCHLÜSSELUNG";
$alpha = "AÜÄÖÇBCDEFGHIJKLMNOPQRSTUYVWXYZ";
$additional_letters = " -.,;!?%&0123456789";
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.=" "; else $word_line.=$word[$x];
}
else { $word_line.="_<font size=1> </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 " 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";
}
?>
对于德语单词,我们需要使用与欧洲单词兼容的 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以更改编码参数。
如今,大多数浏览器都会忽略元内容类型标签。 没有必要使用它们。 您需要在任何 output 之前使用 php 执行实际页面 header:
header("Content-type: text/html; charset=UTF-8");
用这个。
mb_strtolower(mb_convert_encoding($_POST['entered_key'], 'UTF-8', 'UTF-8'), 'UTF-8')
您需要在 HTML 中指定 utf8:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.