Jump to content

[SOLVED] Regex to find and append after HTML FORM Tag


zacware

Recommended Posts

I got saddled with fixing an old web site which contains over 150 files with static contact (name, address, city, etc. ) forms in them (they apparently didn't understand the concept of shared library files?)

 

Anyway, what I'd like to do is find every instance of a html <form> tag that has a specific and exact  "action" attribute and then after the form tag I want to append a hidden html input element with the name of the page so I can get statistics on which pages are calling the common action file.

 

So I want to look for

 

<form_____action="process.php"____>"

 

and replace it with

 

<form_____action="process.php"____><input type=hidden name="source" value="<?php echo $_SERVER[php_SELF]; ?>" />

 

I was thinking I could use php's dom functions to do this, as I'm pretty good with DOM code, but thought it was about time I tried using preg, but after quite a few hours of toying around I just couldn't get anything to work.

 

Thanks in advance for any help.

$subject = ""; // wherever you get the info to be regexed
$replacement = '$0<input type=hidden name="source" value="'.$_SERVER[php_SELF].'" />';
$subject = preg_replace('/(<form.*?action="process.php".*?>)/', $replacement, $subject);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.