Jump to content

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


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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.