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 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!

By dm - May 22, 2014

Just in case anyone else is looking for a similar solution in the future here is the code I found which works for allowing various replacements... 

$string = $selected_word;
$Find = Array($selected_letter, $selected_letter2, $selected_letter3);
$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=\"\" />");

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

echo $New_string;

Cheers!