PeterJ Posted September 7, 2009 Share Posted September 7, 2009 Hi All, I'm using a simple form to submit data to a mysql database - the thing is, everytime I want to do it I take the data (HTML code to be stored in BLOB) first into dreamweaver and run two different find and replaces on it to convert the code from using html formatting to css formatting. It's starting to get tiresome doing this everytime as I submit the form a number of times everyday so I thought perhaps I could get PHP to do the find and replace for me and save me the effort? I'd also like if it could strip out all link tags and the text included in them, but that's perhaps not possible? I use POST to pass the form data to the php script and then simply put it in a variable and INSERT into the DB. Does anyone know if it's possible to add a find and replace type idea so say everytime it passes '<em>' it replaced it with '<span class="item">' . The formatting of the data I'm copying is always the same so I can guarantee that every instance of a certain html tag will equate to a certain CSS style and then I'll be able to control my site from the one stylesheet easier. Thanks Pete Link to comment https://forums.phpfreaks.com/topic/173468-replacing-all-instances-of-string-in-submitted-form/ Share on other sites More sharing options...
TeNDoLLA Posted September 7, 2009 Share Posted September 7, 2009 Everything is possible (almost). Using regexps or DOMDocument you can manipulate HTML data. Maybe provida an example or be a little more specific what you want? Or if its just an instance of some simple string as the topic says probably even php's str_replace() would do. Link to comment https://forums.phpfreaks.com/topic/173468-replacing-all-instances-of-string-in-submitted-form/#findComment-914400 Share on other sites More sharing options...
PeterJ Posted September 8, 2009 Author Share Posted September 8, 2009 Thank you I have the data from the form in a variable via post, I want to scan through the whole variable and replace every instance of <strong><em> with <span class="refNum"> and every instance of </em></strong> with </span>. For links I'd like to delete everything from the opening <a through to the </a> as they refrence materials that are no longer online. So I could perhaps do the basic replacing of strings with? $variable = str_replace("<strong><em>", "<span class=\"refNum\">", $variable); $variable = str_replace("</strong></em> ", "</span>", $variable); Link to comment https://forums.phpfreaks.com/topic/173468-replacing-all-instances-of-string-in-submitted-form/#findComment-914444 Share on other sites More sharing options...
TeNDoLLA Posted September 8, 2009 Share Posted September 8, 2009 This could do it.. <?php $html = '<html> <head> <title>test</title> </head> <body> <a href="test.php" class="test">a test link</a> <p> some text </p> <form action="" method="POST"></form> <p> <strong><em>some text</em></strong> </p> <strong><em>testttting..</em></strong> <a href="test2.php">a test link 2</a><br/> <a href="test3.php">a test link 3</a></br> </body> </html>'; $html = str_replace('<strong><em>', '<span class="refNum">', $html); $html = str_replace('</em></strong>', '</span>', $html); $html = preg_replace('/<a.+>.*<\/a>/', '', $html); echo $html; Link to comment https://forums.phpfreaks.com/topic/173468-replacing-all-instances-of-string-in-submitted-form/#findComment-914632 Share on other sites More sharing options...
PeterJ Posted September 8, 2009 Author Share Posted September 8, 2009 Thanks so much! That is really appreciated - I just tried it out and it's halved the time I spend doing it! Great stuff. Link to comment https://forums.phpfreaks.com/topic/173468-replacing-all-instances-of-string-in-submitted-form/#findComment-915098 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.