PHP Word guessing game (highlight letters in right and wrong position - like mastermind)
<p>Sorry for the long title.<br>
Wanted it to be as descriptive as possible.</p>

<p><em>Disclaimer : Could find some "find the differences" code here and elsewhere on Stackoverflow, but not quite the functionality I was looking for.</em></p>

<p><strong>I'll be using these terminoligy later on:</strong><br>
<strong>'userguess'</strong> : a word that will be entered by the user<br>
<strong>'solution'</strong> : the secret word that needs to be guessed.
<br><br></p>

<h1>What I need to create</h1>

<p>A word guessing game where:
<br></p>

<ul>
<li>The user enters a word (I'll make sure through Javascript/jQuery that
the entered word contains the same number of letters as the word to
be guessed).<br><br></li>
<li>A PHP function then checks the 'userguess' and highlights (in green)
the letters in that word which are in the correct place, and
highlights (in red) the letters that are not yet in the right place,
but do show up somewhere else in the word.<br> The letters that don't
show up in the 'solution' are left black.</li>
</ul>

<p><br><br>
<strong>Pitfall Scenario</strong> : - Let's say the 'solution' is  <strong>'aabbc'</strong> and the user guesses <strong>'abaac'</strong><br>
In the above scenario this would result in : (green)<strong>a</strong>(/green)(red)<strong>b</strong>(/red)(red)<strong>a</strong>(/red)(black)<strong>a</strong>(/black)(green)<strong>c</strong>(/green)<br>
Notice how the last "<strong>a</strong>" is black cause 'userguess' has 3 <strong>a's</strong> but 'solution' only has 2 <br><br></p>

<h1>What I have so far</h1>

<p>Code is working more or less, but I've got a feeling it can be 10 times more lean and mean.<br>
<em>I'm filling up 2 new Arrays (one for solution and one for userguess) as I go along to prevent the pitfall (see above) from messing things up.</em></p>

<pre><code> function checkWord($toCheck) {
        global $solution;      // $solution word is defined outside the scope of this function
        $goodpos = array();    // array that saves the indexes of all the right letters in the RIGHT position
        $badpos  = array();    // array that saves the indexes of all the right letters in the WRONG position
        $newToCheck = array(); // array that changes overtime to help us with the Pitfall (see above)
        $newSolution = array();// array that changes overtime to help us with the Pitfall (see above)

        // check for all the right letters in the RIGHT position in entire string first
        for ($i = 0, $j = strlen($toCheck); $i &lt; $j; $i++) {
           if ($toCheck[$i] == $solution[$i]) {
               $goodpos[] = $i;
               $newSolution[$i] = "*"; // RIGHT letters in RIGHT position are 'deleted' from solution
           } else {
               $newToCheck[] = $toCheck[$i]; 
               $newSolution[$i] = $solution[$i];
           }
        }

        // go over the NEW word to check for letters that are not in the right position but show up elsewhere in the word
        for ($i = 0, $j = count($newSolution); $i &lt;= $j; $i++) {
           if (!(in_array($newToCheck[$i], $newSolution))) {
               $badpos[] = $i;
               $newSolution[$i] = "*";
           } 
        }

        // use the two helper arrays above 'goodpos' and 'badpos' to color the characters
        for ($i = 0, $j = strlen($toCheck), $k = 0; $i &lt; $j; $i++) {
            if (in_array($i,$goodpos)) {
                $colored .= "&lt;span class='green'&gt;";
                $colored .= $toCheck[$i];
                $colored .= "&lt;/span&gt;";
            } else if (in_array($i,$badpos)) {
                $colored .= "&lt;span class='red'&gt;";
                $colored .= $toCheck[$i];
                $colored .= "&lt;/span&gt;";
            } else {
                $colored .= $toCheck[$i];   
            }
        }

        // output to user
        $output  = '&lt;div id="feedbackHash"&gt;';
        $output .= '&lt;h2&gt;Solution was : &amp;nbsp;' . $solution . '&lt;/h2&gt;';
        $output .= '&lt;h2&gt;Color corrected: ' . $colored . '&lt;/h2&gt;';
        $output .= 'Correct letters in the right position : ' . count($goodpos) . '&lt;br&gt;';
        $output .= 'Correct letters in the wrong position : ' . count($badpos)  . '&lt;br&gt;';
        $output .= '&lt;/div&gt;';

        return $output;
    } // checkWord
</code></pre>
