decodelabs/enlighten

PHP source highlighter

v0.9.1 2023-09-26 10:31 UTC

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

PHP source highlighter

Enlighten provides exhaustive and accurate PHP source highlighting for use in debugging systems.

Get news and updates on the DecodeLabs blog.

Installation

Install using Composer:

composer require decodelabs/enlighten

Usage

Enlighten uses the PHP tokenizer extension which requires the full source from file (including <?php open tag).

It is effectively a more thorough version of highlight_string and highlight_file - grammar is properly wrapped and name entities are parsed according to their surrounding tokens to work out what type they are (function name, class name, etc, etc).

Enlighten also offers the ability to extract certain portions of the code and focus on a specific line. Line numbers are included in the output HTML to aid in readability.

use DecodeLabs\Enlighten\Highlighter;

$highlighter = new Highlighter();
echo $highlighter->highlight($phpSourceCode); // Highlight source code in memory
echo $highlighter->highlightFile($phpFile, 15, 35, 20); // Highlight specific lines (15 to 35) in file (focus on 20)

echo $highlighter->extract($phpSourceCode, 20); // Extract code around specific line
echo $highlighter->extractFromFile($phpFile, 20); // Extract code around specific line

Licensing

Enlighten is licensed under the MIT License. See LICENSE for the full license text.