Author: Theodore Brown (theodorejb)
Committer: GitHub (web-flow)
Pusher: saundefined
Date: 2025-11-19T23:44:24+03:00

Commit: 
https://github.com/php/web-php/commit/a32bc943fbdca19f8fa515dfcf7b48f8d52c1fb2
Raw diff: 
https://github.com/php/web-php/commit/a32bc943fbdca19f8fa515dfcf7b48f8d52c1fb2.diff

Support rendering release pages with PHP 8.3+ (#1613)

See https://github.com/php/php-src/pull/11913

Changed paths:
  M  include/layout.inc
  M  styles/theme-base.css


Diff:

diff --git a/include/layout.inc b/include/layout.inc
index 2f3f8207a6..7f572da6bc 100644
--- a/include/layout.inc
+++ b/include/layout.inc
@@ -14,26 +14,33 @@ ini_set('highlight.keyword', 'keyword');
 ini_set('highlight.string',  'string');
 ini_set('highlight.html',    'html');
 
+// convert PHP 8.2 highlight_string() output to match PHP 8.3+
+function normalize_highlight_string(string $html): string
+{
+    $search = ["\r\n", "\n", '<br />', '&nbsp;'];
+    $replace = ["\n", '', "\n", ' '];
+    $result = str_replace($search, $replace, $html);
+
+    // strip extra span tag
+    $result = substr_replace($result, '', 5, 6);
+    $result = substr_replace($result, '', -14, 7);
+
+    return '<pre>' . $result . '</pre>';
+}
+
 // Highlight PHP code
 function highlight_php($code, $return = false)
 {
     $highlighted = highlight_string($code, true);
 
-    // Use this ugly hack for now to avoid code snippets with bad syntax 
screwing up the highlighter
-    if (strstr($highlighted, "include/layout.inc</b>")) {
-        $highlighted = '<span class="html">' . nl2br(htmlentities($code, 
ENT_HTML5), false) . "</span>";
+    if (PHP_VERSION_ID < 80300) {
+        $highlighted = normalize_highlight_string($highlighted);
     }
 
-    // Fix output to use CSS classes and wrap well
-    $highlighted = '<div class="phpcode">' . strtr(
-        $highlighted,
-        [
-            '&nbsp;' => ' ',
-            "\n" => '',
-
-            '<span style="color: ' => '<span class="',
-        ],
-    ) . '</div>';
+    // Fix output to use CSS classes
+    $search = ['<code style="color: ', '<span style="color: '];
+    $replace = ['<code class="', '<span class="'];
+    $highlighted = '<div class="phpcode">' . str_replace($search, $replace, 
$highlighted) . '</div>';
 
     if ($return) { return $highlighted; }
     echo $highlighted;
@@ -45,7 +52,7 @@ function highlight_php_trimmed($code, $return = false)
 {
     $code = "<?php\n" . $code;
     $highlighted_code = highlight_php($code, true);
-    $highlighted_code = preg_replace("!&lt;\?php(<br />)+!", '', 
$highlighted_code, 1);
+    $highlighted_code = preg_replace("!&lt;\?php(\\n)+!", '', 
$highlighted_code, 1);
 
     // add syntax highlighting for variables
     $variableReplacer = function (array $matches) {
diff --git a/styles/theme-base.css b/styles/theme-base.css
index aea0f8f1aa..e35c0e9edc 100644
--- a/styles/theme-base.css
+++ b/styles/theme-base.css
@@ -961,6 +961,9 @@ div.tip p:first-child {
     margin-bottom: 1.5rem;
 }
 
+.phpcode pre {
+    margin: 0;
+}
 .phpcode code {
     display: block;
     overflow-x: auto;

Reply via email to