php和utf-8编码问题与德国字母

时间:2011-07-09 14:16:00

标签: php utf-8 character-encoding

问题:我发现了一个PHP代码(刽子手游戏),我希望它能用于德语,但它输出A BC 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.="&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";
    }
    ?>

5 个答案:

答案 0 :(得分:4)

使用多字节编码时这是错误的:

$alpha[$c]

这将解决$alpha中的一个字节,而不是一个字符。请改用mb_substr

mb_substr($alpha, $c, 1);

答案 1 :(得分:1)

目前,大多数浏览器都会忽略元内容类型标记。没有必要使用它们。在任何输出之前,你需要使用php做一个实际的页面标题:

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

答案 2 :(得分: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来更改编码参数。

答案 3 :(得分:0)

您需要在HTML中指定utf8:

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

答案 4 :(得分:0)

使用它。

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