开发者

php and utf-8 encoding problem with german letters

The problem: i found a php code (hangman game) and i want it to work on german language but it outputs A � � ���B C F instead of these letters : Ü Ä Ö

How can i fix it .. and use only utf-8 encoding ??

here is the whole code :

<?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";
    }
    ?>


This is wrong when working with multibyte encodings:

$alpha[$c]

This will address one byte in $alpha, not one character. Use mb_substr instead:

mb_substr($alpha, $c, 1);


Most browsers these days ignore meta content-type tags. There is no need to use them. You need to do an actual page header using php before any output:

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


For german words we need to use ISO-8859 encoding which is compatible with european words. This solution worked for me before encoding JSON.

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

You can also encode string with UTF-8 for html is ISO-8859-1 doesn't work.

refere this doc http://php.net/manual/en/function.htmlentities.php to change encoding parameters.


you'll need to specify utf8 in your HTML:

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


Use this.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜