目录

PHP - 输出中的代码语法高亮

代码高亮一直以来是程序员的一项基本诉求,已成为各大编辑器、IDE 的标配。当然这个范围可以再扩大些,现在基本上有代码的地方就有代码高亮了,无论你在哪条船上。啥?不支持代码高亮,这不扯犊子吗?作为一个有点扯谈的程序员,我对没有代码语法高亮的这件事是坚决不能忍受的。

内置的高亮函数

字符串的语法高亮

语法高亮的相关配置:

NameDefaultRange
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 - Removed in PHP-5.4.0"#FFFFFF"PHP_INI_ALL
highlight.default#000000"PHP_INI_ALL
highlight.html"#0000BB"PHP_INI_ALL
browscapNULLPHP_INI_SYSTEM
  • 实例一:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php

$string = '';
$string .= "<?php \r\n\r\n";
$string .= "echo 'hello world, hello highlight.';\r\n\r\n";
$string .= "\$array = ['a', 'b', 'c', 'd', 'e'];\r\n";
$string .= "function pr(\$vars)\r\n";
$string .= "{\r\n";
$string .= "\tforeach (\$vars as \$key => \$var) {\r\n";
$string .= "\t\tif (\$key % 2 === 0) {\r\n";
$string .= "\t\t\t\$array[\$key] = \$var . '_test';\r\n";
$string .= "\t\t}\r\n";
$string .= "\t}\r\n";
$string .= "}\r\n";
$string .= 'pr($array);';

echo highlight_string($string, true);

https://inotes.oss-cn-beijing.aliyuncs.com/php/201812/php-highlight-1.png

  • 实例二:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

echo highlight_text(file_get_contents('date.php'));

function highlight_text($text)
{
    ini_set('highlight.comment', '#546E78');
    ini_set('highlight.default', '#DD7455');
    ini_set('highlight.html',
        '#D56162; font-family: Courier; font-weight: bold');
    ini_set('highlight.keyword', '#5083E6');
    ini_set('highlight.string', '#C3E88D');

    $text = trim($text);
    $text = highlight_string('<?php ' . $text, true);
    $text = trim($text);

    // remove custom added "<?php "
    $pattern
        = '|<span style="color: \#[a-zA-Z0-9]{0,6}">&lt;\?php&nbsp;</span>|';
    $text = preg_replace($pattern, '', $text);

    return $text;
}

https://inotes.oss-cn-beijing.aliyuncs.com/php/201812/php-highlight-2.png

  • 实例三:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php highlight(); ?>
  $string = '[ PHP 语法高亮大法好 ]';
<?php highlight(); ?>

<?php
function highlight()
{
    static $on = false;

    if (! $on) {
        ob_start();
    } else {
        $buffer = "<?php\n" . ob_get_contents() . '?>';
        ob_end_clean();
        highlight_string($buffer);
    }
    $on = ! $on;
}

`3` 行输出,我就懒的截图了。

?>

文件的语法高亮

1
2
3
<?php

highlight_file('date.php');

https://inotes.oss-cn-beijing.aliyuncs.com/php/201812/php-highlight-file.png

扩展高亮支持

这种插件比较多,这儿我选择 highlight.php,这款插件支持很多语言的代码高亮,按官方的说法,支持达 176 种语言,无论如何已经够用了。

安装

1
2
// 安装 scrivo/highlight.php
$ composer require scrivo/highlight.php

实例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?php

require './vendor/autoload.php';

$highlight = new \Highlight\Highlighter();
$code = file_get_contents('date.php');
$object = $highlight->highlight('php', $code);

?>
<html>
<head>
  <link rel="stylesheet"
        href="<?= './vendor/scrivo/highlight.php/styles/monokai.css' ?>">
</head>
<body>
<pre class="hljs <?= $object->language ?>"><?= $object->value ?></pre>
</body>
</html>

https://inotes.oss-cn-beijing.aliyuncs.com/php/201812/php-highlight-ext.png