From 963e5c5c092fab5cc5d875bf420951300a439b6b Mon Sep 17 00:00:00 2001 From: AJ Dunn Date: Tue, 8 Sep 2026 12:03:01 +0930 Subject: [PATCH] fix: Remove extra closing PHP tag from highlight_code() on PHP 8.3+. Fix #51. Handle the HTML format introduced in PHP 8.3 when removing the injected closing tag. Keep support for older PHP versions and add a regression test for profiler queries. --- system/helpers/text_helper.php | 4 ++-- tests/codeigniter/helpers/text_helper_test.php | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/system/helpers/text_helper.php b/system/helpers/text_helper.php index 979e1458044..9bb7ace41a9 100644 --- a/system/helpers/text_helper.php +++ b/system/helpers/text_helper.php @@ -343,12 +343,12 @@ function highlight_code($str) $str = preg_replace( array( '/<\?php( | )/i', - '/(.*?)\?><\/span>\n<\/span>\n<\/code>/is', + '/(.*?)\?><\/span>(\n<\/span>\n<\/code>|<\/code><\/pre>)/is', '/<\/span>/i' ), array( '', - "$1\n\n", + '$1$2', '' ), $str diff --git a/tests/codeigniter/helpers/text_helper_test.php b/tests/codeigniter/helpers/text_helper_test.php index 43024abdf32..46baab62f21 100644 --- a/tests/codeigniter/helpers/text_helper_test.php +++ b/tests/codeigniter/helpers/text_helper_test.php @@ -104,7 +104,7 @@ public function test_highlight_code() // PHP 8.3 changed highlight_string() output format if (PHP_VERSION_ID >= 80300) { - $expect = "
<?php var_dump(\$this); ?> ?>
"; + $expect = "
<?php var_dump(\$this); ?> 
"; } else { @@ -112,6 +112,18 @@ public function test_highlight_code() } $this->assertEquals($expect, highlight_code('')); + + // Test code without PHP tags (e.g. database queries in profiler) + if (PHP_VERSION_ID >= 80300) + { + $expect_query = "
SELECT * FROM users; 
"; + } + else + { + $expect_query = "\nSELECT FROM users\n\n"; + } + + $this->assertEquals($expect_query, highlight_code('SELECT * FROM users;')); } // ------------------------------------------------------------------------