HTML to XHTML Converter

来源:互联网 发布:大数据预测性分析 编辑:程序博客网 时间:2024/04/29 08:56
  1. <?php
  2. if (!empty($type)) {
  3.  
  4. if ($type == "path") {
  5.   if (!empty($path)) {
  6.    if (file_exists($path) && is_file($path)) {
  7.     $file = file($path);
  8.     if (substr($file[0],0,9) != "<!DOCTYPE") $doctype=0;
  9.     $file = join('', $file);
  10.    } else {
  11.     die ("No such file.");
  12.    }
  13.   } else {
  14.    die ("No file specified.");
  15.   }
  16. } elseif ($type == "file") {
  17.   if (!empty($file)) {
  18.     
  19.   } else {
  20.    die ("No file specified.");
  21.   }
  22. } else {
  23.   die ("No file specified.");
  24. }
  25.  
  26. # specify html file, check for doctype
  27. //$file = file("file.html");
  28. //if (substr($file[0],0,9) != "<!DOCTYPE") $doctype=1;
  29. //$file = join('', $file);
  30. # make tags and properties lower case, close empty elements, quote all properties
  31. $search  = array ("'(<//?)(/w+)([^>]*>)'e",
  32.                    "'(<//?)(br|input|meta|link|img)([^>]*)( />)'ie",
  33.                    "'(<//?)(br|input|meta|link|img)([^>]*)(/>)'ie",
  34.                    "'(<//?)(br|input|meta|link|img)([^>]*)(>)'ie",
  35.                    "'(/w+=)(/w+)'ie",
  36.                    "'(/w+=)(.+?)'ie");
  37. $replace = array ("'/Ι'.strtolower('/Κ').'/Λ'",
  38.                    "'/Ι/Κ/Λ>'",
  39.                    "'/Ι/Κ/Λ>'",
  40.                    "'/Ι/Κ/Λ //Μ'",
  41.                    "strtolower('/Ι').'/"/Κ/"'",
  42.                    "strtolower('/Ι').'/Κ'");
  43. $file = preg_replace($search, $replace, $file);
  44.  
  45. # return xhtml-compliant document 
  46. echo "<textarea cols=/"100/" rows=/"20/">";
  47. if (isset($doctype)) echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">'."/n";
  48. echo stripslashes(stripslashes(stripslashes($file)));
  49. echo "</textarea>";
  50.  
  51. } else {
  52. ?>
  53. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
  54. <head><title>HTML -> XHTML Convertor</title></head>
  55.  
  56. <body>
  57. <!-- WARNING: this input method is a security risk on open servers //-->
  58. <form action="<?=$PHP_SELF?>" method="get">
  59. <input type="hidden" name="type" value="path" />
  60. <font face="verdana">File path:</font> <input type="text" name="path" size="50" />
  61. <input type="submit" value="Submit" />
  62. </form>
  63.  
  64. <b><font face="verdana">OR</font></b><br /><br />
  65.  
  66. <form action="<?=$PHP_SELF?>" method="get">
  67. <input type="hidden" name="type" value="file" />
  68. <font face="verdana">File contents:</font><br />
  69. <textarea name="file" rows="10" cols="50"></textarea><br />
  70. <input type="submit" value="Submit" />
  71. </form>
  72.  
  73. </body>
  74. </html>
  75. <?
  76. }
  77. ?>