zacware Posted December 17, 2008 Share Posted December 17, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/137327-solved-regex-to-find-and-append-after-html-form-tag/ Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 $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); Quote Link to comment https://forums.phpfreaks.com/topic/137327-solved-regex-to-find-and-append-after-html-form-tag/#findComment-717714 Share on other sites More sharing options...
effigy Posted December 17, 2008 Share Posted December 17, 2008 I would use /(<form[^>]+action="process.php"[^>]*>)/ since greediness is faster. It also constrains the pattern within tag boundaries just in case you encounter some bad HTML. Quote Link to comment https://forums.phpfreaks.com/topic/137327-solved-regex-to-find-and-append-after-html-form-tag/#findComment-717776 Share on other sites More sharing options...
zacware Posted December 17, 2008 Author Share Posted December 17, 2008 Thanks to both of you. You are awesome. Not only does it work perfectly, but seeing the solution really helped me understand how regex works. Have a great holiday. Quote Link to comment https://forums.phpfreaks.com/topic/137327-solved-regex-to-find-and-append-after-html-form-tag/#findComment-717971 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.