ahh sorry, as the substr counts the first char as 0, it needs to go from 0-3
Also, my loop was removing the $i position character rather than a random one... I've put in a method which will return a random number which has not been removed from the string as yet.
I've done some tests and it's working - let me know if you've any queries about the code
function getRandomCharToRemove(array $alreadyRemoved, $strLength)
{
$random = rand(0,$strLength-1); //substr starts from char 0 so we need to subtract 1 from strlen
return (!isset($alreadyRemoved[$random])) ? $random : getRandomCharToRemove($alreadyRemoved,$strLength);
}
$maxCharsToRemove = 2;
$word = 'bees';
$strLength = strlen($word);
$charsGettingRemoved = rand(1,$maxCharsToRemove+1);
$charsRemoved=array();
for ($i=0;$i<$charsGettingRemoved;$i++){
$randomCharToRemove = getRandomCharToRemove($charsRemoved,$strLength);
$charsRemoved[$randomCharToRemove]=substr($word,$randomCharToRemove,1);
$word = substr_replace($word,'*',$randomCharToRemove,1);
}
echo "Your word is: $word<br/>The letters missing are: " . print_r($charsRemoved,1)."<br/>Len: $strLength<br/>Chars removed [position => letter]: $charsGettingRemoved";