Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8223623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:46:18+00:00 2026-06-07T14:46:18+00:00

Sorry for the long title. Wanted it to be as descriptive as possible. Disclaimer

  • 0

Sorry for the long title.
Wanted it to be as descriptive as possible.

Disclaimer : Could find some “find the differences” code here and elsewhere on Stackoverflow, but not quite the functionality I was looking for.

I’ll be using these terminoligy later on:
‘userguess’ : a word that will be entered by the user
‘solution’ : the secret word that needs to be guessed.

What I need to create

A word guessing game where:

  • 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).

  • 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.
    The letters that don’t
    show up in the ‘solution’ are left black.

Pitfall Scenario : – Let’s say the ‘solution’ is ‘aabbc’ and the user guesses ‘abaac’
In the above scenario this would result in : (green)a(/green)(red)b(/red)(red)a(/red)(black)a(/black)(green)c(/green)
Notice how the last “a” is black cause ‘userguess’ has 3 a’s but ‘solution’ only has 2

What I have so far

Code is working more or less, but I’ve got a feeling it can be 10 times more lean and mean.
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.

 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 < $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 <= $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 < $j; $i++) {
            if (in_array($i,$goodpos)) {
                $colored .= "<span class='green'>";
                $colored .= $toCheck[$i];
                $colored .= "</span>";
            } else if (in_array($i,$badpos)) {
                $colored .= "<span class='red'>";
                $colored .= $toCheck[$i];
                $colored .= "</span>";
            } else {
                $colored .= $toCheck[$i];   
            }
        }

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

        return $output;
    } // checkWord
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:46:19+00:00Added an answer on June 7, 2026 at 2:46 pm

    Nice question. I’d probably do it slightly differently to you 🙂 (I guess that’s what you were hoping for!)

    You can find my complete solution function here http://ideone.com/8ojAG – but I’m going to break it down step by step too.

    Firstly, please try and avoid using global. There’s no reason why you can’t define your function as:

    function checkWord($toCheck, $solution) {

    You can pass the solution in and avoid potential nasties later on.

    I’d start by splitting both the user guess, and the solution into arrays, and have another array to store my output in.

    $toCheck = str_split($toCheck, 1);
    $solution = str_split($solution, 1);
    $out = array();
    

    At each stage of the process, I’d remove the characters that have been identified as correct or incorrect from the users guess or the solution, so I don’t need to flag them in any way, and the remaining stages of the function run more efficiently.

    So to check for matches.

    foreach ($toCheck as $pos => $char) {
        if ($char == $solution[$pos]) {
            $out[$pos] = "<span class=\"green\">$char</span>";
            unset($toCheck[$pos], $solution[$pos]);
        }
    }
    

    So for your example guess/solution, $out now contains a green ‘a’ at position 0, and a green c at position 4. Both the guess and the solution no longer have these indices, and will not be checked again.

    A similar process for checking letters that are present, but in the wrong place.

    foreach ($toCheck as $pos => $char) {
        if (false !== $solPos = array_search($char, $solution)) {
            $out[$pos] = "<span class=\"red\">$char</span>";
            unset($toCheck[$pos], $solution[$solPos]);
        }
    }
    

    In this case we are searching for the guessed letter in the solution, and removing it if it is found. We don’t need to count the number of occurrences because the letters are removed as we go.

    Finally the only letters remaining in the users guess, are ones that are not present at all in the solution, and since we maintained the numbered indices throughout, we can simply merge the leftover letters back in.

    $out += $toCheck;

    Almost there. $out has everything we need, but it’s not in the correct order. Even though the indices are numeric, they are not ordered. We finish up with:

    ksort($out);
    return implode($out);
    

    The result from this is:

    "<span class="green">a</span><span class="red">b</span><span class="red">a</span>a<span class="green">c</span>"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for a long question and not a very descriptive title, but my problem
Sorry for the long title, but it seems most descriptive for my question. Basically,
Sorry for the insanely long title, I find this one hard to sum up.
Sorry for the long title. Here is a sample situation that I encounter often
Since the html code is so long and i can't post here. Sorry to
Sorry for the long title. I'm currently writing some 301 redirects and using mod
Sorry for the long and yet still somehow vague title! A friend of mine
Just some background, sorry so long winded. I'm using the System.Data.SQLite ADO.net adapter to
Firstly sorry for the long piece of code pasted below. This is my first
my code is too long so sorry :) @model IEnumerable<Osos11.Models.Customers> @Html.DevExpress().GridView( settings => {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.