Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions system/helpers/text_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,12 @@ function highlight_code($str)
$str = preg_replace(
array(
'/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
'/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
'/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>(\n<\/span>\n<\/code>|<\/code><\/pre>)/is',
'/<span style="color: #[A-Z0-9]+"\><\/span>/i'
),
array(
'<span style="color: #$1">',
"$1</span>\n</span>\n</code>",
'$1</span>$2',
''
),
$str
Expand Down
14 changes: 13 additions & 1 deletion tests/codeigniter/helpers/text_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,26 @@ public function test_highlight_code()
// PHP 8.3 changed highlight_string() output format
if (PHP_VERSION_ID >= 80300)
{
$expect = "<pre><code style=\"color: #000000\"><span style=\"color: #0000BB\">&lt;?php var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">); </span><span style=\"color: #0000BB\">?&gt; ?&gt;</span></code></pre>";
$expect = "<pre><code style=\"color: #000000\"><span style=\"color: #0000BB\">&lt;?php var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">); </span><span style=\"color: #0000BB\">?&gt; </span></code></pre>";
}
else
{
$expect = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\">&lt;?php&nbsp;var_dump</span><span style=\"color: #007700\">(</span><span style=\"color: #0000BB\">\$this</span><span style=\"color: #007700\">);&nbsp;</span><span style=\"color: #0000BB\">?&gt;&nbsp;</span>\n</span>\n</code>";
}

$this->assertEquals($expect, highlight_code('<?php var_dump($this); ?>'));

// Test code without PHP tags (e.g. database queries in profiler)
if (PHP_VERSION_ID >= 80300)
{
$expect_query = "<pre><code style=\"color: #000000\"><span style=\"color: #0000BB\">SELECT </span><span style=\"color: #007700\">* </span><span style=\"color: #0000BB\">FROM users</span><span style=\"color: #007700\">; </span></code></pre>";
}
else
{
$expect_query = "<code><span style=\"color: #000000\">\n<span style=\"color: #0000BB\">SELECT&nbsp;</span><span style=\"color: #007700\">*&nbsp;</span><span style=\"color: #0000BB\">FROM&nbsp;users</span><span style=\"color: #007700\">;&nbsp;</span>\n</span>\n</code>";
}

$this->assertEquals($expect_query, highlight_code('SELECT * FROM users;'));
}

// ------------------------------------------------------------------------
Expand Down
Loading