login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > PHP2HTML

PHP2HTML

Auteur: MindPrison - 12 november 2005 - 17:34 - Gekeurd door: nemesiskoen - Hits: 4141 - Aantal punten: 5.00 (2 stemmen)





Dit script kun je gebruiken om php code snel te highligten om dan in een html document te gebruiken.
vb: Je schrijft een readme in html, met daarin php code die gewoon moet weergegeven worden.

Het werkt heel simpel: uploaden, php code in het verster plakken en op "Converteer" klikken. Je krijgt de html code dan als download.

Je kan het script ook gewoon vanop mijn website gebruiken: http://scripts....p2html.php

De reden waarom het script zo lang is, is het feit dat ik wou dat de geproduceerde html code er zo proper mogelijk uit zag. Er zijn dus zeker stukken code die je kan weggelaten...

Code:
  1. <?php
  2. if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST["php"]))
  3. {
  4. // Moeten slashes weg?
  5. if(get_magic_quotes_gpc() || ini_get("magic_quotes_sybase"))
  6. $_POST["php"] = stripslashes($_POST["php"]);
  7.  
  8. // Highlighten + splitsen per lijn
  9. $zoek = array("\r", "<br />");
  10. $vervang = array("\n", "");
  11. $code = explode("\n", str_replace($zoek, $vervang, highlight_string(trim($_POST["php"]), TRUE)));
  12.  
  13. // Onnodige lijnen verwijderen
  14. $verwijder = array("</span>", "</font>", "</code>");
  15. while(empty($code[(sizeof($code) - 1)]) || in_array($code[(sizeof($code) - 1)], $verwijder))
  16. unset($code[(sizeof($code) - 1)]);
  17.  
  18. // Output code starten
  19. $html = "<!-- Code gemaakt door PHP2HTML v2.2 -->\n";
  20. $html .= "<div style=\"display: block; overflow: scroll; width: " . (ctype_digit($_POST["breedte"]) ? $_POST["breedte"] . "px" : "100%") . "; padding-right: 10px;\">\n";
  21.  
  22. // Standaard tekst kleur ophalen + eerste en laatste lijn verwijderen
  23. preg_match('/<code><(span|font) (style="color: |color=")(#[A-Z0-9]{6})">/', $code[0], $kleur);
  24. $txt_kleur = $kleur[3];
  25. unset($kleur, $code[0]);
  26.  
  27. // Lijn per lijn de code proper maken
  28. for($i = 1; $i <= sizeof($code); $i++)
  29. {
  30. // Code proper maken
  31. $code[$i] = preg_replace('/<(span|font) (style="color: |color=")(#[A-Z0-9]{6})">((&nbsp;)*)/', '\\4<span style="color: \\3;">', $code[$i]);
  32. $code[$i] = preg_replace('/(&nbsp;)*<\/(span|font)>/', '</span>\\1', $code[$i]);
  33.  
  34. // De tag werd niet op dezelfde lijn gesloten
  35. if(sizeof(explode("<span ", $code[$i])) > sizeof(explode("</span>", $code[$i])) && isset($code[($i + 1)]))
  36. {
  37. // Huidige kleur ophalen
  38. preg_match_all('/<span style="color: (#[A-Z0-9]{6});">/', $code[$i], $kleur, PREG_SET_ORDER);
  39.  
  40. // Volgende lijn bepalen
  41. $t = 1;
  42. while(empty($code[($i + $t)])) $t++;
  43.  
  44. // Open tag voor kleur toevoegen
  45. $code[($i + $t)] = "<span style=\"color: " . $kleur[(sizeof($kleur) - 1)][1] . "\">" . $code[($i + $t)];
  46.  
  47. $code[$i] .= "</span>"; // Huidige lijn sluiten
  48. }
  49. elseif(sizeof(explode("<span ", $code[$i])) > sizeof(explode("</span>", $code[$i])))
  50. $code[$i] .= "</span>"; // Span op huidige lijn sluiten, er is geen volgende lijn
  51.  
  52. // Overbodige tags en spaties wegdoen
  53. $code[$i] = preg_replace('/<span style="color: #[A-Z0-9]{6};"><\/span>/', '', $code[$i]);
  54. $code[$i] = preg_replace('/^(&nbsp;)+$/', '', $code[$i]);
  55. }
  56.  
  57. // Met lijnnummering & wordwrap
  58. if(!empty($_POST["lijnnr"][0]) && !empty($_POST["wordwrap"][0]))
  59. {
  60. $html .= "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n"; // Tabel openen
  61.  
  62. foreach($code as $key => $lijn)
  63. {
  64. $html .= "<tr>\n";
  65. $html .= "<td valign=\"top\" align=\"right\" style=\"color: #000000; background-color: #CCCCCC; padding: 0px 2px 0px 10px;\"><code>" . $key . "</code></td>\n";
  66. $html .= "<td valign=\"top\" align=\"left\" style=\"color: " . $txt_kleur . "; padding-left: 5px;\">" . (!empty($lijn) ? "<code>" . preg_replace("/([ ]{2,})/e", "str_replace(' ', '&nbsp;', '\\1');", preg_replace("/^([ ]+)/e", "str_replace(' ', '&nbsp;', '\\1');", str_replace("&nbsp;", " ", $lijn))) . "</code>" : "") . "</td>\n";
  67. $html .= "</tr>\n";
  68. }
  69.  
  70. $html .= "</table>\n"; // Tabel sluiten
  71. }
  72. // Met lijnnummering & zonder wordwrap
  73. elseif(!empty($_POST["lijnnr"][0]) && empty($_POST["wordwrap"][0]))
  74. {
  75. $nr = $bron = "";
  76. foreach($code as $key => $lijn)
  77. {
  78. $nr .= "<br />" . $key;
  79. $bron .= "<br />\n" . $lijn;
  80. }
  81.  
  82. $html .= "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
  83. $html .= "<tr>\n";
  84. $html .= "<td valign=\"top\" align=\"right\" style=\"color: #000000; background-color: #CCCCCC; padding: 0px 2px 0px 10px;\"><code>" . substr($nr, 6) . "</code></td>\n";
  85. $html .= "<td valign=\"top\" align=\"left\" style=\"color: " . $txt_kleur . "; padding-left: 5px;\"><code>" . substr($bron, 7) . "</code></td>\n";
  86. $html .= "</tr>\n";
  87. $html .= "</table>\n";
  88. }
  89. // Zonder lijnnummering
  90. else
  91. {
  92. $html .= "<code><span style=\"color: " . $txt_kleur . ";\">\n";
  93. foreach($code as $key => $lijn) $html .= $lijn . "<br />" . ((isset($code[($key + 1)]) && empty($code[($key + 1)])) ? "" : "\n");
  94. $html .= "</span></code>";
  95. }
  96.  
  97. // HTML i.p.v XHTML -> "<br />" vervangen
  98. if($_POST["type"] == "HTML")
  99. $html = str_replace("<br />", "<br>", $html);
  100.  
  101. // Einde code output
  102. $html .= "</div>\n";
  103. $html .= "<!-- Einde PHP2HTML code -->";
  104.  
  105. // Header instellen en bestand versturen
  106. header("Content-disposition: attachment; filename=php2html.html");
  107. header("Content-Type: text/html");
  108. header("Content-Transfer-Encoding: binary");
  109. header("Content-Length: " . strlen($html));
  110. header("Pragma: no-cache");
  111. header("Expires: 0");
  112. echo $html;
  113. }
  114. else
  115. {
  116. ?>
  117.  
  118. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  119. <html xmlns="http://www.w3.org/1999/xhtml">
  120. <head>
  121.  
  122. <title>PHP2HTML v2.2</title>
  123.  
  124. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  125.  
  126. <style type="text/css">
  127. <!--
  128. body
  129. {
  130. margin: 10px;
  131. font-size: 12px;
  132. font-family: Arial, Helvetica, sans-serif;
  133. color: #333333;
  134. background-color: #CCCCCC;
  135. }
  136. table.pagina
  137. {
  138. width: 600px;
  139. border: 1px solid #000000;
  140. background-color: #FFFFFF;
  141. }
  142. form
  143. {
  144. margin: 0px;
  145. padding: 0px;
  146. }
  147. span.titel
  148. {
  149. font-size: 25px;
  150. font-weight: bold;
  151. }
  152. a:link, a:active, a:visited
  153. {
  154. color: #333333;
  155. text-decoration: underline;
  156. }
  157. a:hover
  158. {
  159. color: #000000;
  160. text-decoration: overline;
  161. }
  162.  
  163. -->
  164. </style>
  165.  
  166. </head>
  167. <body>
  168.  
  169. <form action="<?php echo $_SERVER["REQUEST_URI"]; ?>" method="post" name="php2html">
  170. <table class="pagina" align="center" cellspacing="0" cellpadding="10">
  171. <tr>
  172. <td style="border-bottom: 1px solid #000000;" colspan="2" align="center"><span class="titel">PHP2HTML</span></td>
  173. </tr>
  174. <tr>
  175. <td style="padding-bottom: 0px;" align="center" colspan="2"><textarea name="php" cols="60" rows="20"></textarea></td>
  176. </tr>
  177. <tr>
  178. <td align="right" width="40%" style="padding-bottom: 0px;">Nummer de lijnen:</td>
  179. <td style="padding-bottom: 0px;"><input name="lijnnr[0]" type="checkbox" value="ja" checked="checked" /></td>
  180. </tr>
  181. <tr>
  182. <td align="right" valign="top" width="40%" style="padding-top: 5px; padding-bottom: 0px;">Output:</td>
  183. <td style="padding-top: 2px; padding-bottom: 2px;"><input name="type" type="radio" value="XHTML" checked="checked" style="position: relative; top: 2px;" /> XHTML 1.0<br /><input name="type" type="radio" value="HTML" style="position: relative; top: 2px;" /> HTML 4.01</td>
  184. </tr>
  185. <tr>
  186. <td align="right" valign="top" width="40%" style="padding-top: 5px; padding-bottom: 0px;">Wordwrap:</td>
  187. <td style="padding-top: 5px; padding-bottom: 0px;"><input name="wordwrap[0]" type="checkbox" value="ja" /></td>
  188. </tr>
  189. <tr>
  190. <td align="right" valign="top" width="40%" style="padding-top: 5px;">Maximum breedte:</td>
  191. <td style="padding-top: 5px;"><input name="breedte" type="text" value="Optioneel" size="7" maxlength="9" onclick="if(this.value='Optioneel') this.value = '';" style="font-size: 11px; padding: 0px;" />px</td>
  192. </tr>
  193. <tr>
  194. <td style="border-top: 1px solid #000000;" align="center" colspan="2"><input type="submit" value="Converteer" />&nbsp;&nbsp;&nbsp;<input type="reset" value="Wis alles" /></td>
  195. </tr>
  196. </table>
  197. </form>
  198.  
  199. <div align="center" style="margin-top: 3px;">&copy; <?php echo date("Y"); ?>-<?php echo (date(Y) + 1); ?> <a href="http://www.mvaprojects.be" target="_blank">MVAPROJE&copy;TS</a></div>
  200.  
  201. </body>
  202. </html>
  203.  
  204. <?php
  205. }
  206. ?>
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (4)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.042s