Sevencolors studies:
experiments with Javascript, CSS, PHP and web standards

Printer icon

Tag replacer

Replaces the less-then and greater-then symbols with entities.

This is the PHP code in the body of the Tag replacer page (simplified as far as headers and paragraphs are concerned):

<?php if(isset($_POST['submit'])):
@require_once('writer.php');
?>
<?php else:

echo ("<form method=\"post\" action=\"writer.php\">");
echo ("<label>Paste code:<br />");
echo ("<textarea name=\"code\" rows=\"20\" cols=\"50\"></textarea></label><br />");
echo ("<p><input checked type=\"radio\" name=\"tag\" value=\"br\" /> <br /> tags</p>");
echo ("<p><label><input type=\"radio\" name=\"tag\" value=\"pre\" /> <pre> tags</p>");
echo ("<p><label><input type=\"radio\" name=\"tag\" value=\"allbr\" /> all characters (<br />)<br />");
echo ("<p><label><input type=\"radio\" name=\"tag\" value=\"allpre\" /> all characters (<pre>)<br />");
echo ("<input type=\"submit\" name=\"submit\" value=\"submit\" />");
echo ("</form>");
echo ("<p><em>After clicking the submit button, view the source of the page and copy it.</em></p>");
endif;
?>

It is basically just a form made of a textarea, four radio buttons and a submit button. If the submit button is clicked then the code pasted in the textarea will go through this code:

<?php
$code = $_POST['code'];
$t = $_POST['tag'];
switch($t):
case 'br':
$s = array("<",">","\r");
$l = array("<",">","<br />");
$rep_code = str_replace($s,$l,$code);
$strip = stripslashes($rep_code);
echo ($strip);
break;
case 'pre':
$s = array("<",">");
$l = array("<",">");
$rep_code = str_replace($s,$l,$code);
$strip = stripslashes($rep_code);
echo ("<pre>".$strip."</pre>");
break;
case 'allbr':
$encoded = htmlentities($code,ENT_QUOTES);
$s = array("\r");
$l = array("<br />");
$rep_code = str_replace($s,$l,$encoded);
$strip = stripslashes($rep_code);
echo ($strip);
break;
case 'allpre':
$encoded = htmlentities($code,ENT_QUOTES);
$s = array("\r");
$l = array("<br />");
$rep_code = str_replace($s,$l,$encoded);
$strip = stripslashes($rep_code);
echo ("<pre>".$strip."</pre>");
break;
endswitch;
?>

..and be displayed after having had all his less-then and greater-then symbols replaced with entities or, choosing the all characters option, all HTML characters translated into entities, including quotes (then view the source, copy , paste the code and publish it). If you choose to use the <br /> tags there will be no tags around the code. If you choose to wrap it into <pre> tags then whitespace will be preserved automatically. I use a switch because is faster then a series of if and else if.

It is very useful and saves a lot of boring manual replacing.

TRY IT !