I have a secret word. People need to guess the secret word by entering single letters. When they start it will have **** for the word. When they guess a letter, an asterick shoud be replaced with the correctly guessed letter.
I cant get the charachter to replace in the correct position or keep the astericks visible. Also there should be hidden field for for the secret word, but when i make it hiddden I don't know how to output the replaced string. Any help would be great.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Hangman</title>
  <link rel="stylesheet" href="teams.css" type="text/css" />
  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
  $word = 'rocky';
  $secret = '*****';
  $letter;

     echo "<p>Enter a lower case letter and click the Submit button to guess the word</P>";

     if (isset($_POST['Submit'])) {
        $letter = $_POST['letter'];
      $secret = $_POST['secret'];
       for ($i=0; $i<strlen($word); $i++) {
            if($word[$i] == $letter){
              $pos =  strpos($word, $letter);
               $secret = substr_replace($secret, $letter, $pos);
            }

        }
    }

 ?>

 <form name="Word Guess" action="Guess19373.php" method="POST">
 <p>Enter a letter: <input type="text" name="letter" value="<?php echo $letter; ?>" /></p>
 <p>The secret word is: <input type="text" name="secret" value="<?php echo $secret; ?>"  /></p>
 <p><input type="submit" name="Submit" value="Give it a guess" /></p>
 </form>


</body>
</html>
Member Avatar for iamthwee

I would do something like

        $arrayName = 'guessword';

        $guess = 'e';

        for ($i=0; $i < strlen($arrayName); $i++) 
        { 
            if ($guess == $arrayName[$i])
            {
                echo $arrayName[$i];
            }
            else
                echo '*';
         }  
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.