I want to use regular expressions to replace input tags with their corresponding values. The search must include the name of the input, since I have multiple inputs in the same form that need replacing.
For example, I need the following string:
<input value="Your Name" name="custom_field_1">
to become:
Your Name
My current code looks like this:
foreach ($_POST as $key => $value) {
if($key == 'post_id') continue;
$content = preg_replace("/<\s* input [^>]+ >/xi", htmlspecialchars($value), $content);
}
However, this will replace ALL inputs with the first value, so I need to refine the regex to include the name.
Thank you very much for your help!
- Ciprian
<input value="Your Name" name="custom_field__29541">? Maybe, you showvar_dum($_POST)?array(7) { ["post_id"]=> string(4) "4495" ["custom_field_1"]=> string(7) "Value 1" ["custom_field_2"]=> string(7) "Value 2" ["custom_field_3"]=> string(7) "Value 3" ["custom_field_4"]=> string(7) "Value 4" ["custom_field_5"]=> string(7) "Value 5" ["custom_field_6"]=> string(7) "Value 6" }echo $_POST["post_id"]; // 4495You don't need any replacingecho $_POST["custom_field_1"]; // Value 1$content = preg_replace("#<input([^>]*)name=['\"]".preg_quote($key)."['\"]([^>]*)>#Uis", htmlspecialchars($value), $content);Thank you for all your help!