0

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
5
  • Why $_POST contains html <input value="Your Name" name="custom_field__29541"> ? Maybe, you show var_dum($_POST) ? Commented Jul 8, 2015 at 18:09
  • var_dump for $_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" } Commented Jul 8, 2015 at 18:58
  • echo $_POST["post_id"]; // 4495 You don't need any replacing Commented Jul 8, 2015 at 19:05
  • echo $_POST["custom_field_1"]; // Value 1 Commented Jul 8, 2015 at 19:06
  • I found a regex that works: $content = preg_replace("#<input([^>]*)name=['\"]".preg_quote($key)."['\"]([^>]*)>#Uis", htmlspecialchars($value), $content); Thank you for all your help! Commented Jul 8, 2015 at 19:12

2 Answers 2

1

You can match the whole string, capture the value by using parenthesis (..), making a capture group, and replacing the match with $1 - the first capture group.

<input value="(.+?)" name=".*?">

https://regex101.com/r/rH1wA2/3

Try: preg_replace("<input value=\\"(.+?)\\" name=\\".*?\\">", $1, $str)

Note that this regex has "value", "name" and even spaces. If your strings sometimes look different, you have to take that to account and change the regex accordingly. <input value="(.+?)"[^>]*>, for example, to replace no matter what follows your desired value.

Sign up to request clarification or add additional context in comments.

Comments

1

This should work.
Just replace the match with $1

 # (?s)<input(?=\s)(?>(?:(?<=\s)value\s*=\s*"([^"]+)"|".*?"|'.*?'|[^>]*?)+>)(?(1)|(?!))

 (?s)
 <input                        # Input tag
 (?= \s )
 (?>                           # Atomic grouping
      (?:
           (?<= \s )
           value  \s* = \s*              # 'value' attribute
           "
           (                             # (1 start), 'value' data
                [^"]+ 
           )                             # (1 end)
           "
        |  " .*? "
        |  ' .*? '
        |  [^>]*? 
      )+
      >
 )
 (?(1)                         # Conditional - Only input tags with a 'value' attr
   |  (?!)
 )

Sample:

<input type="hidden" value="Special:Search" name="title" />

Output:

 **  Grp 0 -  ( pos 0 , len 59 ) 
<input type="hidden" value="Special:Search" name="title" />  
 **  Grp 1 -  ( pos 28 , len 14 ) 
Special:Search  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.