[DOC-CVS] [phd] syntax/absent-php-tag: handle PHP code snippets without opening tag by pretending it exists

[email protected] (Jordi Kroon)
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: Jordi Kroon (jordikroon)
Date: 2026-08-23T20:08:26+02:00

Commit: https://github.com/php/phd/commit/44914abe140e0ef8e6a73e04a23e3a5c6adec693
Raw diff: https://github.com/php/phd/commit/44914abe140e0ef8e6a73e04a23e3a5c6adec693.diff

handle PHP code snippets without opening tag by pretending it exists

Changed paths:
  A  tests/highlighter_php_no_open_tag.phpt
  M  phpdotnet/phd/Highlighter.php


Diff:

diff --git a/phpdotnet/phd/Highlighter.php b/phpdotnet/phd/Highlighter.php
index c59c18e7..1235a178 100644
--- a/phpdotnet/phd/Highlighter.php
+++ b/phpdotnet/phd/Highlighter.php
@@ -62,8 +62,17 @@ public function highlight($text, $role, $format)
         }
 
         if ($role == 'php') {
+            $origText = $text;
+            $prepended = false;
+            if (strpos($text, '<?') === false) {
+                $text = "<?php\n" . $text;
+                $prepended = true;
+            }
             try {
                 $highlight = highlight_string($text, true);
+                if ($prepended) {
+                    $highlight = preg_replace('/&lt;\?php(?:<br \/>|\n)/', '', $highlight, 1);
+                }
                 if (PHP_VERSION_ID >= 80300) {
                     return $highlight;
                 } else {
@@ -73,10 +82,10 @@ public function highlight($text, $role, $format)
                     ]);
                 }
             } catch (\ParseException $e) {
-                trigger_error(vsprintf("Parse error while highlighting PHP code: %s\nText: %s", [(string) $e, $text]), E_USER_WARNING);
+                trigger_error(vsprintf("Parse error while highlighting PHP code: %s\nText: %s", [(string) $e, $origText]), E_USER_WARNING);
 
                 return '<pre class="'. $role . 'code">'
-                    . htmlspecialchars($text, ENT_QUOTES, 'UTF-8')
+                    . htmlspecialchars($origText, ENT_QUOTES, 'UTF-8')
                     . "</pre>\n";
             }
         } else {
diff --git a/tests/highlighter_php_no_open_tag.phpt b/tests/highlighter_php_no_open_tag.phpt
new file mode 100644
index 00000000..4edb96b0
--- /dev/null
+++ b/tests/highlighter_php_no_open_tag.phpt
@@ -0,0 +1,19 @@
+--TEST--
+Highlighter colors PHP snippets that lack a <?php open tag
+--SKIPIF--
+<?php if (PHP_VERSION_ID < 80300) die("skip highlight_string() output differs before PHP 8.3"); ?>
+--FILE--
+<?php
+namespace phpdotnet\phd;
+
+require_once __DIR__ . "/setup.php";
+
+$highlighter = Highlighter::factory("xhtml");
+
+echo $highlighter->highlight('$kitty->eat($banana);', "php", "xhtml"), "\n";
+echo $highlighter->highlight("<?php\n\$kitty->eat(\$banana);", "php", "xhtml"), "\n";
+?>
+--EXPECT--
+<pre><code style="color: #000000"><span style="color: #0000BB">$kitty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">eat</span><span style="color: #007700">(</span><span style="color: #0000BB">$banana</span><span style="color: #007700">);</span></code></pre>
+<pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
+$kitty</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">eat</span><span style="color: #007700">(</span><span style="color: #0000BB">$banana</span><span style="color: #007700">);</span></code></pre>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.