PHP 8.5.0 Alpha 1 available for testing

Voting

: three plus one?
(Example: nine)

The Note You're Voting On

will at lolcat dot ca
2 years ago
Here is the best way to highlight code if you want classes instead of inline styles:

<?php
function highlightcode($text){

ini_set("highlight.comment", "c-comment");
ini_set("highlight.default", "c-default");
ini_set("highlight.html", "c-default");
ini_set("highlight.keyword", "c-keyword");
ini_set("highlight.string", "c-string");

$text =
trim(
str_replace(
'<br />',
"\n", // replace <br> with newlines
str_replace(
[
// leading <?php garbage
"<span style=\"color: c-default\">\n&lt;?php&nbsp;",
"<code>",
"</code>"
],
"",
highlight_string("<?php " . $text, true)
)
)
);

// replace colors
$classes = ["c-comment", "c-default", "c-keyword", "c-string"];

foreach(
$classes as $class){

$text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
}

return
$text;
}
?>

Generates output that looks like this:

<span class="c-keyword">if(</span>condition<span class="c-keyword">){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span class="c-string">"HTML&nbsp;here"</span><span class="c-keyword">;
}</span>

Hope this helps.

<< Back to user notes page

To Top