<?
if (!empty($type)) {
if ($type == "path") {
if (!empty($path)) {
if (file_exists($path) && is_file($path)) {
$file = file($path);
if (substr($file[0],0,9) != "<!doctype") $doctype=0;
$file = join(, $file);
} else {
die ("no such file.");
}
} else {
die ("no file specified.");
}
} elseif ($type == "file") {
if (!empty($file)) {
} else {
die ("no file specified.");
}
} else {
die ("no file specified.");
}
# specify html file, check for doctype
//$file = file("file.html");
//if (substr($file[0],0,9) != "<!doctype") $doctype=1;
//$file = join(, $file);
# make tags and properties lower case, close empty elements, quote all properties
$search = array ("(<\/?)(\w+)([^>]*>)e",
"(<\/?)(br|input|meta|link|img)([^>]*)( />)ie",
"(<\/?)(br|input|meta|link|img)([^>]*)(/>)ie",
"(<\/?)(br|input|meta|link|img)([^>]*)(>)ie",
"(\w+=)(\w+)ie",
"(\w+=)(.+?)ie");
$replace = array ("\\1.strtolower(\\2).\\3",
"\\1\\2\\3>",
"\\1\\2\\3>",
"\\1\\2\\3 /\\4",
"strtolower(\\1).\"\\2\"",
"strtolower(\\1).\\2");
$file = preg_replace($search, $replace, $file);
# return xhtml-compliant document
echo "<textarea cols=\"100\" rows=\"20\">";
if (isset($doctype)) echo <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "dtd/xhtml1-transitional.dtd">."\n";
echo stripslashes(stripslashes(stripslashes($file)));
echo "</textarea>";
} else {
?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "dtd/xhtml1-transitional.dtd">
<head><title>html -> xhtml convertor</title></head>
<body>
<!– warning: this input method is a security risk on open servers //–>
<form action="<?=$php_self?>" method="get">
<input type="hidden" name="type" value="path" />
<font face="verdana">file path:</font> <input type="text" name="path" size="50" />
<input type="submit" value="submit" />
</form>
<b><font face="verdana">or</font></b><br /><br />
<form action="<?=$php_self?>" method="get">
<input type="hidden" name="type" value="file" />
<font face="verdana">file contents:</font><br />
<textarea name="file" rows="10" cols="50"></textarea><br />
<input type="submit" value="submit" />
</form>
</body>
</html>
<?
}
?>
原文自www.evilwalrus.com
