Jump to content

Replace a letter with multiple instances in a word once in php array


kents

Recommended Posts

I have created a word game in php/mysql where users are presented with a randomly selected word in a foreign language and then have to fill in missing letters (which depending on the word length are sometimes randomly removed or sometimes all letters are removed) from the english version of these words into form input fields which are later submitted to the database via a form for checking...

The code below works ok except for when a word has more than one instance of a particular letter for examples the word "Bees"

For example it will go through find "e" but then replace both instances of the letter "e" with either

<input type="text" name="question_1_letter_1" class="inputs" maxlength="1" value="" />

or

<input type="text" name="question_1_letter_2" class="inputs" maxlength="1" value="" />

 

What I really want to happen is for it to just replace once the first "e" it finds so one instance of the letter "e" is replaced by

 

<input type="text" name="question_1_letter_1" class="inputs" maxlength="1" value="" />

 

and the other instance is replaced with

 

<input type="text" name="question_1_letter_2" class="inputs" maxlength="1" value="" />

 

here is the main code

$one = "question_1_letter_$key1";
$two = "question_1_letter_$key2";
$three = "question_1_letter_$key3";
$four = "question_1_letter_$key4";
$string = Bees;
$selected_letter1 = B;
$selected_letter2 = e;
$selected_letter3 = e;
$selected_letter4 = s;

$Find = Array($selected_letter1, $selected_letter2, $selected_letter3, $selected_letter4);

$Replace = Array(
"`<input type=\"text\" name=\"$one\" class=\"inputs\" maxlength=\"1\" value=\"\" />`",
"`<input type=\"text\" name=\"$two\" class=\"inputs\" maxlength=\"1\" value=\"\" />`",
"`<input type=\"text\" name=\"$three\" class=\"inputs\" maxlength=\"1\" value=\"\" />`",
"`<input type=\"text\" name=\"$four\" class=\"inputs\" maxlength=\"1\" value=\"\" />`"
);

$New_string = strtr($string, array_combine($Find, $Replace));

echo $New_string;

 

 

Hope that makes sense! Im still very much a novice php programmer and stuck as to how I can achieve this and may well be overlooking a much simpler solution that I am currently unaware of... if anyone can lend a hand and give me some pointers it would be greatly appreciated!!

Link to comment
Share on other sites

session_start();
$_SESSION['currentWord'] = 'bees';
$word = $_SESSION['currentWord'];
$length = strlen($word);
$maxCharsToRemove = 2; //you could swap this for a algorithm based on the strlen (i.e. ceil($length/3)) -- lets round up incase the division is less than 0

$charsGettingRemoved = rand(1,$maxCharsToRemove);
$charsRemoved=array();
for ($i=1;$i<=$charsGettingRemoved;$i++){
    $charsRemoved[$i]=substr($word,$i,1);
    $word = substr_replace($word,'*',$i,1);
}

echo "Your word is: $word<br/>The letters missing are: " . print_r($charsRemoved,1)."<br/>Len: $length<br/>Chars: $charsGettingRemoved";

//and when teh users posts the form, concatenate all of the posted characters and see if it equals to $_SESSION['currentWord']

and then for the html

<input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" />
<input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" />
<input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" />
<input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" />

the above html will be accessible in an array of $_POST['question_1_letters'] and will automatically receive indexes... so you can implode and see that it equals to the question 1 word

if (implode('',$_POST['question_1_letters'])==$_SESSION['currentWord'])
//they got it right
else
//they got it wrong

apologies for rewriting your code, i started out fixing yours though thought it easier to understand written this way... and easier to scale :)

Edited by joel24
Link to comment
Share on other sites

thanks very much for taking the time to look at this joel24 much appreciated!

 

i will try to have a go at changing my code to approach it as per your suggestion and see if i have any success

 

one quick question if you have the time, how do i get the above code to take out all the letters if the word is "bees"

 

changing $maxCharsToRemove = 2 to $maxCharsToRemove = 4

 

seems to skip the first letter of the word...

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

  • 2 weeks later...

hi joel24

 

apologies for the delay in replying i have only just had the opportunity to revisit this and your suggestions have really been a massive help so wanted to say thanks very much for your time!

 

i am left with one small issue which i can't seem to get my head around if yourself or anyone here can give me some pointers it would be much appreciated...

for ($i=0;$i<$charsGettingRemoved;$i++){
$randomCharToRemove = getRandomCharToRemove($charsRemoved,$strLength);
$charsRemoved[$randomCharToRemove]=substr($word,$randomCharToRemove,1);
$word = substr_replace($word,'*',$randomCharToRemove,1);
}

in your code above i understand that this part

$word = substr_replace($word,'*',$randomCharToRemove,1);
is where it replaces the randomly removed character with a star '*'
 
instead of a star '*' i would like the removed characters to be replaced with:
 
<input type="text" name="question_1_letters[]" class="inputs" maxlength="1" value="" />
so i can display the full word with a mixture of unremoved characters and inputs where characters have been removed eg B[input]e[input], [input]ees, B[input][input]s, etc etc so the user just has to enter those characters which have been randomly removed, then it goes on to check if there were correct...
 
however if i change the code to
for ($i=0;$i<$charsGettingRemoved;$i++){
$randomCharToRemove = getRandomCharToRemove($charsRemoved,$strLength);
$charsRemoved[$randomCharToRemove]=substr($word,$randomCharToRemove,1);
$word = substr_replace($word,'<input type=\"text\" name=\"question_1_letters[]\" class=\"inputs\" maxlength=\"1\" value=\"\" />',$randomCharToRemove,1);
}

sometimes it works and other times it seems to replace parts of the input html code breaking it in the process...

 

i assume i'm missing something big here but really not sure how to have the code echo the input html rather than the *

 

many thanks in advance for any help!

 

Edited by kents
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.