Syntax highlighting for PHP projects
Server-side highlighting for the full PHP stack: 27 languages covering PHP, HTML, Twig, JavaScript, CSS, YAML, and more. Zero dependencies, semantic PHP syntax understanding definitions vs. calls, embedded language support, and 490+ compatible themes. Works in Twig templates, Laravel Blade, Symfony controllers—anywhere PHP runs.
- Built for PHP ecosystems: Highlight the full stack—PHP, HTML, Twig, JavaScript, CSS, SQL, YAML, and 20+ more languages in a single library.
- Zero dependencies: No Node.js, Python, or external processes. Just Composer. Works in any PHP 8.4+ environment.
- Full PHP syntax: Semantic parser that understands context—distinguishes
class User(definition) fromnew User()(usage),function greet()( definition) fromgreet()(call). - Embedded languages: Automatically switches parsers for
<style>and<script>tags in HTML, fenced code blocks in Markdown, and{% block css %}in Twig. - Dark mode support: Multiple dark themes included out of the box ( CupertinoDark, Dracula, Noctis) plus compatibility with 490+ Highlight.js and Prism themes.
composer require alto/code-highlightuse Alto\Code\Highlight\Highlighter;
use Alto\Code\Highlight\Theme\GitHubTheme;
// 1. Initialize with a theme
$highlighter = new Highlighter(new GitHubTheme());
// 2. Output the theme's CSS (typically in your <head>)
echo "<style>" . $highlighter->getTheme()->getStylesheet() . "</style>";
// 3. Highlight your code
echo $highlighter->highlight($code, 'php');Alto uses a semantic parser for PHP that goes beyond pattern matching to understand code context. It correctly distinguishes between:
- Definitions vs. usage:
class Uservs.new User(),function greet()vs.greet() - Context-aware scoping: Variables, function calls, class instantiation, method calls
Four languages support automatic embedded language detection:
- HTML — CSS in
<style>tags, JavaScript in<script>tags - SVG — CSS in
<style>tags, JavaScript in<script>tags - Markdown — Any language in fenced code blocks (
```language) - Twig — Languages via block names (
{% block css %},{% block javascript %})
| Category | Languages |
|---|---|
| Programming | Bash, C#, Go, Java, JavaScript, PHP, Python, Ruby, Rust, Swift, TypeScript |
| Markup | HTML, SVG, XML |
| Data | Diff, DotEnv, HTTP, JSON, YAML |
| Prose | Markdown |
| Query | SQL |
| Stylesheet | CSS, SCSS |
| Template | Twig |
| Config | Dockerfile, INI, Makefile |
Alto includes 7 built-in themes ready to use:
- Light themes:
Alto,GitHub,Polar,Solar - Dark themes:
CupertinoDark,Dracula,Noctis
use Alto\Code\Highlight\Theme\DraculaTheme;
$highlighter = new Highlighter(new DraculaTheme());Three dark themes are included out of the box:
- CupertinoDark — macOS-inspired dark theme
- Dracula — Popular dark theme with vibrant colors
- Noctis — Low-contrast dark theme for extended coding sessions
Use existing CSS from the Highlight.js (240+ themes), Prism (250+ themes), or TextMate (.tmTheme) ecosystems:
use Alto\Code\Highlight\Adapter\HighlightJsThemeAdapter;
$theme = HighlightJsThemeAdapter::fromFile('/path/to/github-dark.css');
$highlighter = new Highlighter($theme);use Alto\Code\Highlight\Adapter\TextMateThemeAdapter;
$theme = TextMateThemeAdapter::fromFile('/path/to/monokai.tmTheme', isDark: true);
$highlighter = new Highlighter($theme);Twig Extension — Highlight code directly in Twig templates using blocks or filters.
Contributions are welcome! Please feel free to submit issues or pull requests.
Released by the Alto project under the MIT License. See the LICENSE file for details.