The source of html.php (click to demo the file) viewed 2198 times.
If I wrote this code, then it is licensed under the GPL. If someone else wrote it, then please ask them if you want to use the code.
<?php
function realsafehtml($str) {
// Don't do anything if there's no difference or if the original string is empty
$oldstr = "";
while($str != $oldstr) // Loop until it got no more effect
{
$oldstr = $str;
//nuke script and header tags and anything inbetween
$str = preg_replace("'<script[^>]*?>.*?</script>'si", "", $str);
$str = preg_replace("'<head[^>]*?>.*?</head>'si", "", $str);
//listed of tags that will not be striped but whose attributes will be
$allowed = "";
//start nuking those suckers. don you just love MS Word's HTML?
$str = preg_replace("/<((?!\/?($allowed)\b)[^>]*>)/xis", "", $str);
$str = preg_replace("/<($allowed).*?>/i", "<\\1>", $str);
}
return $str;
}
$test = "<br>hello;@<test!££%£$|\/?;> /#?>M\",./#';:[{]}=+-_)(*&^%$£!%<html>";
$test = preg_replace('/[^a-zA-Z0-9]/','',$test);
echo $test ."<br>";
echo realsafehtml($test);
?>
If you want to have a look at the source code, chose a file from this list:
To colour code your own PHP paste it here: