Wednesday, January 24, 2007

Escape Utility

Sometimes you'll find yourself needing an escaped string. Just so you don't have to write one yourself, here's a simple PHP page to help you escape and unescape large strings.

This can also come in handy if you need to get convert large documents into xml/html friendly versions. If you take a gander of the source of a page (from a browser) after you've converted. You will find between the <textarea> tags, html friendly text. Enjoy ;)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>NiX0n's Escape Utility</title>
<style type="text/css">
<!--
table { width: 100%; }
th, td { width: 50%; }
textarea
{
display: block;
height: 500px;
width: 100%;
overflow: scroll;
}
-->
</style>
</head>

<body>
<form action="escape.php" method="post">
<table>
<tr>
<th>Unescaped</th><th>Escaped</th>
</tr>
<tr>
<td>
<textarea name="unescaped"><?php
switch(isset($_POST['escape'])?$_POST['escape']:false)
{
case "escape":
echo htmlspecialchars($_POST['unescaped']);
break;
case "unescape":
echo htmlspecialchars(stripslashes($_POST['escaped']));
case false:
default:
break;
}
?></textarea>
<input name="escape" value="escape" type="submit" style="float:right;" />
</td>
<td>
<textarea name="escaped"><?php
switch(isset($_POST['escape'])?$_POST['escape']:false)
{
case "escape":
echo htmlspecialchars(addslashes($_POST['unescaped']));
break;
case "unescape":
echo htmlspecialchars($_POST['escaped']);
case false:
default:
break;
}
?></textarea>
<input name="escape" value="unescape" type="submit" style="float:left;" />
</td>
</tr>
</table>
</form>
</body>
</html>

Labels:

0 Comments:

Post a Comment

<< Home