alto/code-highlight

Server-side syntax highlighting for PHP with semantic scopes, embedded languages, and adaptable themes.

Maintainers

Package info

github.com/altophp/code-highlight

Homepage

Documentation

pkg:composer/alto/code-highlight

Transparency log

Fund package maintenance!

smnandre

Statistics

Installs: 67

Dependents: 1

Suggesters: 0

Stars: 1

Open Issues: 0

v1.0.0 2026-08-09 20:56 UTC

This package is auto-updated.

Last update: 2026-08-13 03:59:13 UTC


README

Server-side syntax highlighting for PHP applications, with semantic scopes, embedded languages, and no third-party PHP package dependencies at runtime.

  PHP Version   CI   Packagist   License   GitHub Sponsors

PHP highlighted with the Alto Dark theme

Core highlighting runs entirely in PHP. It needs no browser runtime, Node.js process, or external service. Its parsers assign semantic scopes, so themes can distinguish a function definition from a call or a type definition from a reference.

Installation

Install ALTO Code Highlight with Composer:

composer require alto/code-highlight

ALTO Code Highlight requires PHP 8.4 or later, Mbstring, and Tokenizer. Tokenizer is included with PHP; Mbstring is available in most PHP distributions but must be enabled.

See the installation guide for verification and troubleshooting.

Quick Start

<?php

use Alto\Code\Highlight\Highlighter;
use Alto\Code\Highlight\Theme\AltoTheme;

$theme = new AltoTheme();
$highlighter = new Highlighter($theme);
$code = '<?php echo "Hello, Alto!";';

echo '<style>'.$theme->getStylesheet().'</style>';
echo $highlighter->highlight($code, 'php');

highlight() returns escaped HTML inside <pre class="alto-highlight"><code>…</code></pre>. Emit a theme stylesheet once per page, then reuse the highlighter for every code block.

What it covers

  • 27 languages: the PHP web stack plus common programming, markup, data, configuration, and query languages.
  • Semantic highlighting: context-aware scopes for definitions, calls, types, variables, constants, and other language concepts.
  • Embedded languages: CSS and JavaScript in HTML/SVG, fenced code in Markdown, and language blocks in Twig.
  • Line controls: optional line numbers and selected-line emphasis.
  • 12 built-in variants: seven theme families, including Alto, GitHub, Dracula, Polar, Cupertino, Noctis, and Solar.
  • Theme compatibility: adapters for Highlight.js CSS, Prism CSS, and TextMate .tmTheme files.

Documentation

Guide Contents
Documentation index Choose the right guide
Getting started Complete rendering, line numbers, and errors
Languages Exact identifiers and language capabilities
Themes Built-in variants and visual examples
Create a theme Implement ThemeInterface
Embedded languages HTML, SVG, Markdown, and Twig
Theme adapters Highlight.js, Prism, and TextMate
Public API Supported entry points and extension contracts
Examples Compact examples and generated previews

The complete source examples are available in examples/languages/.

Languages

Use the lowercase identifier in the second argument to highlight():

bash        csharp       css          diff         dockerfile
dotenv      go           html         http         ini
java        javascript   json         makefile     markdown
php         python       ruby         rust         scss
sql         svg          swift        twig         typescript
xml         yaml

The special php-snippet identifier accepts PHP without an opening <?php tag. The language reference documents exact behavior and embedded-language support.

Line numbers and highlighted lines

$html = $highlighter->highlight(
    code: $code,
    language: 'php',
    lineNumbers: true,
    highlightLines: [2, 3],
);

Choose a theme

use Alto\Code\Highlight\Theme\GitHubTheme;

$light = new GitHubTheme(dark: false);
$dark = new GitHubTheme();

Browse the built-in theme matrix, learn how to create a theme, or reuse an existing stylesheet through a theme adapter.

Integrations

alto/twig-code-highlight adds blocks and filters for Twig applications. The core package remains framework-independent and can be used in Symfony controllers, Laravel views, static generators, or any PHP rendering pipeline.

Contributing

Contributions of all kinds are welcome. Visit the project on GitHub to report a bug, suggest a feature, or open a pull request.

Before submitting code, run:

# Runs PHP CS Fixer, PHPStan, and PHPUnit
composer qa

Language parsers use fixtures under tests/Language/. Public showcase examples live separately under examples/languages/; they are short documentation samples rather than exhaustive parser tests.

Support

ALTO Code Highlight is open source. You can support its continued development through GitHub Sponsors.

Sharing this package with others or starring it on GitHub is also much appreciated.

License

ALTO Code Highlight is released by ALTO PHP under the MIT License.