note 128322 added to function.highlight-file
| From: | rga156 at gmail dot com | Date: | Sun, 19 Mar 2023 11:38:31 +0000 |
| Subject: | note 128322 added to function.highlight-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220280@lists.php.net to get a copy of this message | ||
I change all the 'style="color:#nnnnnn"' attributes to
'class="classname"' and use ini_get() to identify which names go with which
colors:
<?php
$arCodes = [
'comment' => ini_get('highlight.comment'),
'default' => ini_get('highlight.default'),
'html' => ini_get('highlight.html'),
'keyword' => ini_get('highlight.keyword'),
'string' => ini_get('highlight.string'),
];
?>
The code to do the replacement is based on the following:
<?php
$newstr = str_replace ("<span style=\"color: $colorcode\">",
"<span class=\"$classname\">", $str);
?>
To get the browser to do the work of adding line numbers, change the <br /> tags to <li>
tags and add <ol> and </ol> tags:
<?php
if (false===$src = highlight_file($fpath, true) {
die ("Unable to read file '$fpath'");
}
$NL = PHP_EOL; // To work on both Linux and Windows
// Following code assumes that styles have been replaced with classes as above
$numstr = str_replace (
[ '<code><span class="html">',
"</span>$NL</code>", '<br />'],
[ '<code><ol><li><span class="html">',
"</span></ol>$NL</code>", '<li>'],
$src );
?>
Then use CSS to format how you want.
For efficiency, I put all the replacement parameters into an array and do all the str_replace()
actions in one pass. The class substitutions need to be first in the array in order for the
<ol> replacement to work.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 2a00:23c4:2600:6c01:eb90:4be9:5c6e:c965)
----
Manual Page -- https://php.net/manual/en/function.highlight-file.php
Edit -- https://main.php.net/note/edit/128322
Del: integrated -- https://main.php.net/note/delete/128322/integrated
Del: useless -- https://main.php.net/note/delete/128322/useless
Del: bad code -- https://main.php.net/note/delete/128322/bad+code
Del: spam -- https://main.php.net/note/delete/128322/spam
Del: non-english -- https://main.php.net/note/delete/128322/non-english
Del: in docs -- https://main.php.net/note/delete/128322/in+docs
Del: other reasons-- https://main.php.net/note/delete/128322
Reject -- https://main.php.net/note/reject/128322
Search -- https://main.php.net/manage/user-notes.php