return php using echo preg_replace?

Hi all,

I am building a kind of vocabulary hangman type game using php preg_replace to remove random letters from words and replace them with text input fields which when the users enter what they think the missing letter is should be submitted to my mysql database for checking.

I am rather lost on how to convert the input “value” to echo php code needed to import the letter to my cms db…

In my php file I have the following:

[php]<? echo preg_replace("/$selected_letter/", "", $str); ?>[/php]

This echoes out into my page:

with no problems

What I really need to echo is the following:

Doing the following doesn’t works as I am unable to echo [php]<?php echo htmlspecialchars(@$_REQUEST['test']) ?>[/php] within the preg_replace echo if that makes sense…

[php]<? echo preg_replace("/$selected_letter/", "" />", $str); ?>[/php]

I would greatly appreciate it if someone could point me in the right direction as to how I can acheive this?

Many Thanks!

Instead of using preg_replace why don’t you use str_split?

An example:

[php]<?php

$word = “Wheel of Fortune”;
$answer = str_split($word);

echo ‘

’;
print_r($answer);[/php]

That way each individual character would be put it an array and you can have another variable called $hidden or $randomLetters?
[php]$randomLetters = $answer[/php] that way you can replace the word(s) with random letters.

You could write a function for $randomLetters replacing the correct letters with random letters, then you can have the user check it with the correct word(s) [variable -> $answer]?

This might help or not? :slight_smile:

Break it down in logical sections that way you can create functions that will give you the result that you want.

  1. Grab the word
  2. Split the word up into an array
  3. Assign a correct answer variable and an hidden/random variable, then randomize the random variable.
  4. Have the user guess by inputting the letters (or words) depending on how you have input setup and check it against the correct answer variable.
    5 …

Creating a hangman or a vocabulary hangman game you need to know the answer first (the coder) and jumble it up from there. Making sure you save the correct answer first in a separate variable. I think your logic is backwards, unless I’m misunderstanding it. I have written a couple of Hangman style games in Flash Actionscript 3 in the past, so I’m very familiar with them.

Sponsor our Newsletter | Privacy Policy | Terms of Service