HTML to XHTML Converter

本文介绍了一个简单的PHP脚本,该脚本可以将HTML文件转换为XHTML格式。通过检查文档类型声明并进行必要的标签和属性转换,确保输出符合XHTML标准。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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. ?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值