3 * takes a dir as arg, highlights all php code found in html files inside.
5 * @author Gaetano Giunta
6 * @copyright (c) 2007-2015 G. Giunta
8 function highlight($file)
10 $starttag = '<pre class="programlisting">';
13 $content = file_get_contents($file);
16 while (($start = strpos($content, $starttag, $last)) !== false) {
17 $end = strpos($content, $endtag, $start);
18 $code = substr($content, $start + strlen($starttag), $end - $start - strlen($starttag));
19 if ($code[strlen($code) - 1] == "\n") {
20 $code = substr($code, 0, -1);
23 $code = str_replace(array('>', '<'), array('>', '<'), $code);
24 $code = highlight_string('<?php ' . $code, true);
25 $code = str_replace('<span style="color: #0000BB"><?php <br />', '<span style="color: #0000BB">', $code);
27 $out = $out . substr($content, $last, $start + strlen($starttag) - $last) . $code . $endtag;
28 $last = $end + strlen($endtag);
30 $out .= substr($content, $last, strlen($content));
37 $files = scandir($dir);
38 foreach ($files as $file) {
39 if (substr($file, -5, 5) == '.html') {
40 $out = highlight($dir . '/' . $file);
41 file_put_contents($dir . '/' . $file, $out);