diff --git a/system/Debug/BaseExceptionHandler.php b/system/Debug/BaseExceptionHandler.php
index 33dd126ff1ec..e90f95408c24 100644
--- a/system/Debug/BaseExceptionHandler.php
+++ b/system/Debug/BaseExceptionHandler.php
@@ -182,8 +182,11 @@ protected static function highlightFile(string $file, int $lineNumber, int $line
$source = str_replace(["\r\n", "\r"], "\n", $source);
$source = explode("\n", highlight_string($source, true));
- $source = str_replace('
', "\n", $source[1]);
- $source = explode("\n", str_replace("\r\n", "\n", $source));
+
+ if (PHP_VERSION_ID < 80300) {
+ $source = str_replace('
', "\n", $source[1]);
+ $source = explode("\n", str_replace("\r\n", "\n", $source));
+ }
// Get just the part to show
$start = max($lineNumber - (int) round($lines / 2), 0);
diff --git a/tests/system/Debug/ExceptionHandlerTest.php b/tests/system/Debug/ExceptionHandlerTest.php
index 1321f67195f3..8276c76c7ffb 100644
--- a/tests/system/Debug/ExceptionHandlerTest.php
+++ b/tests/system/Debug/ExceptionHandlerTest.php
@@ -237,4 +237,29 @@ public function testMaskSensitiveDataTraceDataKey(): void
$this->assertSame('/var/www/CodeIgniter4/app/Controllers/Home.php', $newTrace[0]['file']);
}
+
+ public function testHighlightFile(): void
+ {
+ $highlightFile = $this->getPrivateMethodInvoker($this->handler, 'highlightFile');
+
+ $output = $highlightFile(APPPATH . 'Controllers/Home.php', 9, 3);
+
+ if (PHP_VERSION_ID >= 80300) {
+ $expect = <<< 'HTML'
+
8 {
+ 9 return view('welcome_message');
+ 10 }
+
+ HTML;
+ } else {
+ $expect = <<< 'HTML'
+ 8 {
+ 9 return view('welcome_message');
+ 10 }
+
+ HTML;
+ }
+
+ $this->assertSame($expect, $output);
+ }
}