To pass more than one css rule, but also a class, an id, or any html attribute to highlighted element, we can escape a quote to close the inline styling:
ini_set('highlight.string', '#F8F8F8 ; font-size:1.4em\" class=\'string\' ');
PHP - Manual: 运行时配置
2024-12-22
这些函数的行为受 php.ini 中的设置影响。
名字 | 默认 | 可修改范围 | 更新日志 |
---|---|---|---|
ignore_user_abort | "0" | PHP_INI_ALL | |
highlight.string | "#DD0000" | PHP_INI_ALL | |
highlight.comment | "#FF8000" | PHP_INI_ALL | |
highlight.keyword | "#007700" | PHP_INI_ALL | |
highlight.bg | "#FFFFFF" | PHP_INI_ALL | 在PHP 5.4.0.中已移除 |
highlight.html | "#0000BB" | PHP_INI_ALL | |
highlight.html | "#000000" | PHP_INI_ALL | |
browscap | NULL | PHP_INI_SYSTEM |
这是配置指令的简短说明。
ignore_user_abort
boolean
默认值为 false
。 如果设置为 true
,在客户端断开连接后,脚本不会被中止。
highlight.bg
string
highlight.comment
string
highlight.default
string
highlight.html
string
highlight.keyword
string
highlight.string
string
语法高亮的颜色。可设置为 <font color="??????"> 中任何可接受的代码。
browscap
string
浏览器功能文件的位置和文件名 (例如 browscap.ini)。 参见 get_browser()。
To pass more than one css rule, but also a class, an id, or any html attribute to highlighted element, we can escape a quote to close the inline styling:
ini_set('highlight.string', '#F8F8F8 ; font-size:1.4em\" class=\'string\' ');
*** Additions to the previous message ***
To change the style directly from a CSS file, it is recommended to only go through classes instead of hard-coding it.
<?php
// Create an array with default php functions
$functions = array("default", "html", "keyword", "string", "comment");
// Replace color code (ex: #FF8000) with class name (ex:"highlight-comment")
foreach ($functions as $value) {
ini_set("highlight.$value", "highlight-$value;");
}
$content = highlight_file($filename, true);
// or
$content = highlight_string($string, true);
// Convert style="color: highlight-function" into class="highlight-function" into $content
// It allows you to modify only the default functions
foreach ($functions as $value) {
$content = preg_replace("/style=\"color: highlight-$value;\"/", "class=\"highlight-$value\"", $content);
}
?>
And in the CSS file (for example) :
.highlight-html { color: #000000; }
.highlight-default { color: #0000bb; }
.highlight-keyword { color: #007700; font-weight: bold; }
.highlight-string { color: #dd0000; }
.highlight-comment { color: #ff8000; }
To change the style directly from a CSS file, it is recommended to only go through classes instead of hard-coding it.
<?php
$functions = array("default", "html", "keyword", "string", "comment");
foreach ($functions as $value) {
ini_set("highlight.$value", "highlight-$value;");
}
?>
And in the CSS file (for example) :
.highlight-html { color: #000000; }
.highlight-default { color: #0000bb; }
.highlight-keyword { color: #007700; font-weight: bold; }
.highlight-string { color: #dd0000; }
.highlight-comment { color: #ff8000; }