echo php within preg_replace to allow database update?

4 posts by 2 authors in: Forums > CMS Builder
Last Post: May 22, 2014   (RSS)

By dm - May 13, 2014

Hi all,

I am using cms builder to make a kind of vocabulary hangman type game... using php preg_replace to remove random letters from words stored in the cms database and replacing them with text input fields which when the users enter what they think the missing letter is should be which are then submitted to my mysql database for checking as per the addform.php example found on this forum.

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:

<? echo preg_replace("/$selected_letter/", "<input type=\"text\" class=\"inputs\" maxlength=\"1\" value=\"\" />", $str); ?>

This echoes out into my page with no problems:

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

What I really need to echo is the following (as per the addform.php):

<input type="text" class="inputs" maxlength="1" value="<?php echo htmlspecialchars(@$_REQUEST['test']) ?>" />

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

<? echo preg_replace("/$selected_letter/", "<input type=\"text\" class=\"inputs\" maxlength=\"1\" value=\"<?php echo htmlspecialchars(@$_REQUEST['test']) ?>\" />", $str); ?>

I would greatly appreciate it if someone more skilled in php could point me in the right direction as to how/if I can acheive this? Maybe preg_replace is not the answer here but I have spent many days searching and not found a closer alternative so far..

Many Thanks!

By gregThomas - May 16, 2014

Hi dm,

I notice that your input doesn't have a name attribute, could the issue be that when you submit the form the value isn't being passed because no name has been set in the input? Changing the input to the following should create $_REQUEST['test'] when the form is submitted:

<input type="text" class="inputs" name="test" maxlength="1" value="<?php echo htmlspecialchars(@$_REQUEST['test']) ?>" />

Does that resolve the issue?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By dm - May 20, 2014

Hi Greg,

Sorry for the late reply, thanks ever so much for taking the time to reply, it is really appreciated!

I actually found a dirty work around by bringing the value in using an include from a set field in the database but your way works much much neater, can't believe I didn't spot the name attrribute! Cheers!

One last question if I may:

Words over a certain size have 3 letters taken out (instead of just 1) and are replaced by 3 different input fields each with different name attributes

As suggested by example 2 on the php manual pages http://es1.php.net/preg_replace I have tried using arrays to achieve this but where it loops through it changes all the instances of the selected letter (ie if "i" is a selected letter it will also strip out i from input and the i from my class input) rather than just the one from the original string.

$string = $selected_word;
$patterns = array();
$patterns[0] = "/$selected_letter/";
$patterns[1] = "/$selected_letter2/";
$patterns[2] = "/$selected_letter3/";
$replacements = array();
$replacements[2] = "<input type=\"text\" name=\"test3\" class=\"inputs\" maxlength=\"1\" value=\"\" />";
$replacements[1] = "<input type=\"text\" name=\"test2\" class=\"inputs\" maxlength=\"1\" value=\"\" />";
$replacements[0] = "<input type=\"text\" name=\"test1\" class=\"inputs\" maxlength=\"1\" value=\"\" />";
echo preg_replace($patterns, $replacements, $string);

I know I can choose to replace more than one pattern with the same replacement and if the array route isnt doable I was hoping that it would be possible to do something like this below to define multiple things to replace and multiple replacement values but I can't seem to find any way of doing this...

ie something like this: echo preg_replace("/$selected_letter|$selected_letter2|$selected_letter3/", "<input type=\"text\" class=\"inputs\" maxlength=\"1\" />", $str);

Do you know of a way around the array issue or an easier way to achieve the same result?

Once again thanks very much for the awesome support!