Jump to content

Replacing all instances of string in submitted form...


PeterJ

Recommended Posts

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

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.

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

 

 

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;

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.