fork(5) download
  1. <?php
  2.  
  3. function checkWord($toCheck, $solution) {
  4. $toCheck = str_split($toCheck, 1);
  5. $solution = str_split($solution, 1);
  6. $out = array();
  7.  
  8. foreach ($toCheck as $pos => $char) {
  9. if ($char == $solution[$pos]) {
  10. $out[$pos] = "<span class=\"green\">$char</span>";
  11. unset($toCheck[$pos], $solution[$pos]);
  12. }
  13. }
  14.  
  15. foreach ($toCheck as $pos => $char) {
  16. if (false !== $solPos = array_search($char, $solution)) {
  17. $out[$pos] = "<span class=\"red\">$char</span>";
  18. unset($toCheck[$pos], $solution[$solPos]);
  19. }
  20. }
  21.  
  22. $out += $toCheck;
  23. ksort($out);
  24.  
  25. return implode($out);
  26. }
  27.  
  28. var_dump(checkWord('abaac', 'aabbc'));
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
string(109) "<span class="green">a</span><span class="red">b</span><span class="red">a</span>a<span class="green">c</span>"