Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Polskie znaki
Forum PHP.pl > Forum > Przedszkole
123tomek
Witam!
Czy mógłby ktoś sprawdzić czy poniższy skrypt, po dodaniu polskich ogonków, działa prawidłowo czy ja coś mam pomieszane na AppServ.

  1. <?php
  2. /*
  3. #####################################################################
  4. # PHP Hangman Game #
  5. # Version 1.2.0 #
  6. # Š2002,2003 0php.com - Free PHP Scripts #
  7. #####################################################################
  8.  
  9. #####################################################################
  10. # #
  11. # Author : 0php.com #
  12. # Created : July 12, 2002 #
  13. # Modified : March 22, 2004 #
  14. # E-mail : webmaster@0php.com #
  15. # Website : <a href="http://www.0php.com/" target="_blank">http://www.0php.com/</a> #
  16. # License : FREE (GPL); See Copyright and Terms below #
  17. # #
  18. # Donations accepted via PayPal to webmaster@0php.com #
  19. # #
  20. #####################################################################
  21.  
  22. >> Copyright and Terms:
  23.  
  24. This software is copyright (C) 2002-2004 0php.com. It is distributed
  25. under the terms of the GNU General Public License (GPL). Because it is licensed
  26. free of charge, there is NO WARRANTY, it is provided AS IS. The author can not
  27. be held liable for any damage that might arise from the use of this software.
  28. Use it at your own risk.
  29.  
  30. All copyright notices and links to 0PHP.com website MUST remain intact in the scripts and in the HTML for the scripts.
  31.  
  32. For more details, see <a href="http://www.0php.com/license_GNU_GPL.php" target="_blank">http://www.0php.com/license_GNU_GPL.php</a> (or <a href="http://www.gnu.org/)" target="_blank">http://www.gnu.org/)</a>.
  33.  
  34. >> Installation
  35. Copy the PHP script and images to the same directory. You can optionally edit the category and list of words/phrases to solve below. You can also add additional characters to $additional_letters and/or $alpha if you want to use international (non-English) letters or other characters not included by default (see further instructions below for those).
  36.  
  37. To prevent Google from playing hangman, add the line below between <HEAD> and </HEAD>:
  38. <META NAME="robots" CONTENT="NOINDEX,NOFOLLOW">
  39.  
  40.  
  41. ================================================================================
    =======*/
  42.  
  43.  
  44. $Category = "Web Programming";
  45.  
  46. # list of words (phrases) to guess below, separated by new line
  47. $list = "JAVA BEANS
  48. PHP SCRIPTS
  49. SOURCE CODE
  50. JAVASCRIPT GAMES
  51. SSI IS SERVER SIDE INCLUDES
  52. BILL GATES
  53. COOKIES
  54. HTTP AUTHENTICATION
  55. ERROR HANDLING
  56. MANIPULATING IMAGES
  57. FILE UPLOADS
  58. DATABASE CONNECTION
  59. APACHE SERVER
  60. ZIP FILE
  61. TAR COMPRESSION
  62. FUNCTIONS
  63. ENCRYPTION
  64. MYSQL DATABASE
  65. INITIALIZATION
  66. FAQ - FREQUENTLY ASKED QUESTIONS
  67. DEBUGGING
  68. VERIFICATION
  69. HTML VALIDATION
  70. CASCADING STYLE SHEETS";
  71.  
  72.  
  73. # make sure that any characters to be used in $list are in either
  74. # $alpha OR $additional_letters, but not in both. It may not work if you change fonts.
  75. # You can use either upper OR lower case of each, but not both cases of the same letter.
  76.  
  77. # below ($alpha) is the alphabet letters to guess from.
  78. # you can add international (non-English) letters, in any order, such as in:
  79. # $alpha = "ŔÁÂĂÄĹĆÇČÉĘËĚÍÎĎŃŇÓÔŐÖŘŮÚŰÜÝźABCDEFGHIJKLMNOPQRSTUVWXYZ";
  80. $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  81.  
  82. # below ($additional_letters) are extra characters given in words; '?' does not work
  83. # these characters are automatically filled in if in the word/phrase to guess
  84. $additional_letters = " -.,;!?%&0123456789";
  85.  
  86. #========= do not edit below here ======================================================
  87.  
  88.  
  89. echo<<<endHTML
  90. <HTML><HEAD><TITLE>Play PHP Hangman Game</TITLE>
  91. <META NAME="DESCRIPTION" CONTENT="A Free PHP Script Š www.0php.com">
  92. <meta content="text/html; charset=windows-1252" http-equiv=content-type>
  93. <meta http-equiv="Content-Style-Type" content="text/css">
  94. <style type="text/css">
  95. <!--
  96. H1 {font-family: "Courier New", Courier, monospace; font-size: 18pt;}
  97. P {font-family: Verdana, Arial, sans-serif; font-size: 12pt;}
  98. A:link {COLOR: #0000FF; TEXT-DECORATION: underline;}
  99. A:visited {COLOR: #0000FF; TEXT-DECORATION: underline;}
  100. A:active {COLOR: #0000FF; TEXT-DECORATION: underline;}
  101. A:hover {COLOR: #FF0000; TEXT-DECORATION: underline;}
  102. -->
  103. </style>
  104. </HEAD>
  105.  
  106. <BODY bgColor="#CCCCCC" link="navy" vlink="navy" alink="navy">
  107. <DIV ALIGN="center">
  108. endHTML;
  109.  
  110. $len_alpha = strlen($alpha);
  111.  
  112. if(isset($_GET["n"])) $n=$_GET["n"];
  113. if(isset($_GET["letters"])) $letters=$_GET["letters"];
  114. if(!isset($letters)) $letters="";
  115.  
  116. if(isset($PHP_SELF)) $self=$PHP_SELF;
  117. else $self=$_SERVER["PHP_SELF"];
  118.  
  119. $links="";
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. $max=6; # maximum number of wrong
  127. # error_reporting(0);
  128. $list = strtoupper($list);
  129. $words = explode("\n",$list);
  130. srand ((double)microtime()*1000000);
  131. $all_letters=$letters.$additional_letters;
  132. $wrong = 0;
  133.  
  134. echo "<P><B>Play PHP Hangman Game</B> &nbsp; - &nbsp; Category: <B>$Category</B><BR>\n";
  135.  
  136. if (!isset($n)) { $n = rand(1,count($words)) - 1; }
  137. $word_line="";
  138. $word = trim($words[$n]);
  139. $done = 1;
  140. for ($x=0; $x < strlen($word); $x++)
  141. {
  142. if (strstr($all_letters, $word[$x]))
  143. {
  144. if ($word[$x]==" ") $word_line.="&nbsp; "; else $word_line.=$word[$x];
  145. }
  146. else { $word_line.="_<font size=1>&nbsp;</font>"; $done = 0; }
  147. }
  148.  
  149. if (!$done)
  150. {
  151.  
  152. for ($c=0; $c<$len_alpha; $c++)
  153. {
  154. if (strstr($letters, $alpha[$c]))
  155. {
  156. if (strstr($words[$n], $alpha[$c])) {$links .= "\n<B>$alpha[$c]</B> "; }
  157. else { $links .= "\n<FONT color=\"red\">$alpha[$c] </font>"; $wrong++; }
  158. }
  159. else
  160. { $links .= "\n<A HREF=\"$self?letters=$alpha[$c]$letters&n=$n\">$alpha[$c]</A> "; }
  161. }
  162. $nwrong=$wrong; if ($nwrong>6) $nwrong=6;
  163. echo "\n<p><BR>\n<IMG SRC=\"hangman_$nwrong.gif\" ALIGN=\"MIDDLE\" BORDER=0 WIDTH=100 HEIGHT=100 ALT=\"Wrong: $wrong out of $max\">\n";
  164.  
  165. if ($wrong >= $max)
  166. {
  167. $n++;
  168. if ($n>(count($words)-1)) $n=0;
  169. echo "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n";
  170. echo "<p><BR><FONT color=\"red\"><BIG>SORRY, YOU ARE HANGED!!!</BIG></FONT><BR><BR>";
  171. if (strstr($word, " ")) $term="phrase"; else $term="word";
  172. echo "The $term was \"<B>$word</B>\"<BR><BR>\n";
  173. echo "<A HREF=$self?n=$n>Play again.</A>\n\n";
  174. }
  175. else
  176. {
  177. echo " &nbsp; # Wrong Guesses Left: <B>".($max-$wrong)."</B><BR>\n";
  178. echo "<H1><font size=5>\n$word_line</font></H1>\n";
  179. echo "<P><BR>Choose a letter:<BR><BR>\n";
  180. echo "$links\n";
  181. }
  182. }
  183. else
  184. {
  185. $n++; # get next word
  186. if ($n>(count($words)-1)) $n=0;
  187. echo "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n";
  188. echo "<P><BR><BR><B>Congratulations!!! &nbsp;You win!!!</B><BR><BR><BR>\n";
  189. echo "<A HREF=$self?n=$n>Play again</A>\n\n";
  190. }
  191.  
  192. echo<<<endHTML
  193.  
  194. <p align="center"><BR><BR><font face="Verdana" size="1">PHP Hangman Game - Version 1.2.0<br>
  195. <a href="http://www.0php.com" target="_top">Free PHP Scripts</a> - Copyright Š <a href="http://www.0php.com" target="_top">0php.com</a> 2002-2004.</font>
  196.  
  197. </DIV></BODY></HTML>
  198.  
  199. endHTML;
  200. ?>



Po dodaniu do poniższej linii polskich ogonków na stronie wyświetlają się u mnie krzaki.
  1. $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Damonsson
Robisz ze stringa tablicę. Nie wziąłeś pod uwagę, że znaki specjalne w UTF-8 zajmują 2 bajty, więc jeden znak będzie pod dwoma kluczami i wyjdą cyrki. Rozbij to sobie przez
  1. $alpha = preg_split('//u', $alpha, -1, PREG_SPLIT_NO_EMPTY);
i powinno grać

Poza tym kodowanie zmień z windows-1252 na utf-8
  1. <meta content="text/html; charset=windows-1252" http-equiv=content-type>


Jeżeli nie jesteś pewien kodowania po stronie serwera, to zrób sobie na początku pliku tak:
  1. <?php
  2. header('Content-Type: text/html; charset=utf-8');
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.