@@ -14,26 +14,33 @@ ini_set('highlight.keyword', 'keyword');
1414ini_set ('highlight.string ' , 'string ' );
1515ini_set ('highlight.html ' , 'html ' );
1616
17+ // convert PHP 8.2 highlight_string() output to match PHP 8.3+
18+ function normalize_highlight_string (string $ html ): string
19+ {
20+ $ search = ["\r\n" , "\n" , '<br /> ' , ' ' ];
21+ $ replace = ["\n" , '' , "\n" , ' ' ];
22+ $ result = str_replace ($ search , $ replace , $ html );
23+
24+ // strip extra span tag
25+ $ result = substr_replace ($ result , '' , 5 , 6 );
26+ $ result = substr_replace ($ result , '' , -14 , 7 );
27+
28+ return '<pre> ' . $ result . '</pre> ' ;
29+ }
30+
1731// Highlight PHP code
1832function highlight_php ($ code , $ return = false )
1933{
2034 $ highlighted = highlight_string ($ code , true );
2135
22- // Use this ugly hack for now to avoid code snippets with bad syntax screwing up the highlighter
23- if (strstr ($ highlighted , "include/layout.inc</b> " )) {
24- $ highlighted = '<span class="html"> ' . nl2br (htmlentities ($ code , ENT_HTML5 ), false ) . "</span> " ;
36+ if (PHP_VERSION_ID < 80300 ) {
37+ $ highlighted = normalize_highlight_string ($ highlighted );
2538 }
2639
27- // Fix output to use CSS classes and wrap well
28- $ highlighted = '<div class="phpcode"> ' . strtr (
29- $ highlighted ,
30- [
31- ' ' => ' ' ,
32- "\n" => '' ,
33-
34- '<span style="color: ' => '<span class=" ' ,
35- ],
36- ) . '</div> ' ;
40+ // Fix output to use CSS classes
41+ $ search = ['<code style="color: ' , '<span style="color: ' ];
42+ $ replace = ['<code class=" ' , '<span class=" ' ];
43+ $ highlighted = '<div class="phpcode"> ' . str_replace ($ search , $ replace , $ highlighted ) . '</div> ' ;
3744
3845 if ($ return ) { return $ highlighted ; }
3946 echo $ highlighted ;
@@ -45,7 +52,7 @@ function highlight_php_trimmed($code, $return = false)
4552{
4653 $ code = "<?php \n" . $ code ;
4754 $ highlighted_code = highlight_php ($ code , true );
48- $ highlighted_code = preg_replace ("!<\?php(<br /> )+! " , '' , $ highlighted_code , 1 );
55+ $ highlighted_code = preg_replace ("!<\?php( \\ n )+! " , '' , $ highlighted_code , 1 );
4956
5057 // add syntax highlighting for variables
5158 $ variableReplacer = function (array $ matches ) {
0 commit comments