Server-side syntax highlighting for PHP applications, with semantic scopes, embedded languages, and no third-party PHP package dependencies at runtime.
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.
Install ALTO Code Highlight with Composer:
composer require alto/code-highlightALTO 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.
<?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.
- 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
.tmThemefiles.
| 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/.
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.
$html = $highlighter->highlight(
code: $code,
language: 'php',
lineNumbers: true,
highlightLines: [2, 3],
);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.
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.
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 qaLanguage parsers use fixtures under tests/Language/. Public showcase examples
live separately under examples/languages/; they are short documentation
samples rather than exhaustive parser tests.
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.
ALTO Code Highlight is released by ALTO PHP under the MIT License.
