note 128657 added to function.highlight-string
| From: | will.lolcat.ca via php-notes | Date: | Tue, 04 Jul 2023 02:00:02 +0000 |
| Subject: | note 128657 added to function.highlight-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220618@lists.php.net to get a copy of this message | ||
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<?php ",
"<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">){
echo </span><span
class="c-string">"HTML here"</span><span
class="c-keyword">;
}</span>
Hope this helps.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.168 (proxied: 66.130.118.35)
----
Manual Page -- https://php.net/manual/en/function.highlight-string.php
Edit -- https://main.php.net/note/edit/128657
Del: integrated -- https://main.php.net/note/delete/128657/integrated
Del: useless -- https://main.php.net/note/delete/128657/useless
Del: bad code -- https://main.php.net/note/delete/128657/bad+code
Del: spam -- https://main.php.net/note/delete/128657/spam
Del: non-english -- https://main.php.net/note/delete/128657/non-english
Del: in docs -- https://main.php.net/note/delete/128657/in+docs
Del: other reasons-- https://main.php.net/note/delete/128657
Reject -- https://main.php.net/note/reject/128657
Search -- https://main.php.net/manage/user-notes.php