定制 graftype/php-highlight 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

graftype/php-highlight

最新稳定版本:1.0.14

Composer 安装命令:

composer require graftype/php-highlight

包简介

A PHP library for highlighting code syntax.

README 文档

README

PHPHighlight is a PHP syntax highlighting library that can be easily customized and extended.

How it works

The library parses the text, finds the <pre> tag, reads the attributes (data-lang, data-file, data-theme) and highlights the syntax of the code block.

Supports style customization. Here are examples of styling:

styling example

Requirements

PHP 8.1+

Installation

You can install package via composer

$ composer require demyanovs/php-highlight

Usage

See full example here index.php

<?php

require_once 'vendor/autoload.php';

use Demyanovs\PHPHighlight\Highlighter;
use Demyanovs\PHPHighlight\Themes\ObsidianTheme;

$text = '
<pre data-file="php-highlight/examples/index.php" data-lang="php">
&lt;?php
abstract class AbstractClass
{
    /**
     * Our abstract method only needs to define the required arguments
     */
    abstract protected function prefixName(string $name): string;
}

class ConcreteClass extends AbstractClass
{
    /**
     * Our child class may define optional arguments not in the parent\'s signature
     */
    public function prefixName(string $name): string
    {
        $prefix = "";
        if ($name === "Pacman") {
            $prefix = "Mr.";
        } elseif ($name === "Pacwoman") {
            $prefix = "Mrs.";
        } else {
            
        }
        
        return $prefix . " " . $name;
    }
}

$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";
</pre>
';

$highlighter = new Highlighter($text, ObsidianTheme::TITLE);
// Configuration
$highlighter->showLineNumbers(true);
$highlighter->showActionPanel(true);
echo $highlighter->parse();

Customization

$highlighter->showLineNumbers(true);
$highlighter->showActionPanel(true);

You can set following attributes in <pre> tag <pre data-lang="php" data-file="example.php" data-theme="drakuala">

  • lang - a language of the text. This affects how the parser will highlight the syntax.
  • file - show file name in action panel.
  • theme - allows to overwrite the global theme.

How to create a custom theme

To create a custom theme you need to create an instance of Demyanovs\PHPHighlight\Themes\Theme class and pass it to Highlighter as a third argument:

$defaultColorSchemaDto = new DefaultColorSchemaDto(...);
$PHPColorSchemaDto = new PHPColorSchemaDto(...);
$XMLColorSchemaDto = new XMLColorSchemaDto(...);

$myTheme = new Theme(
    'myThemeTitle',
    $defaultColorSchemaDto,
    $PHPColorSchemaDto,
    $XMLColorSchemaDto
);

$highlighter = new Highlighter($text, 'myThemeTitle', [$myTheme]);

Supports language syntax

  • PHP
  • JavaScript
  • XML/HTML
  • Bash
  • Go
  • and others

Themes

  • default
  • darkula
  • railscasts
  • obsidian
  • far
  • vs2015
  • c64

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

统计信息

  • 总下载量: 17
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固