PHPackages                             dan-da/highlight.php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. dan-da/highlight.php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dan-da/highlight.php
====================

Server side syntax highlighter that supports 185 languages. It's a PHP port of highlight.js. This fork implements default CLI (ansi) color scheme

v9.15.6.2(6y ago)0707BSD-3-ClausePHP

Since Apr 29Pushed 6y agoCompare

[ Source](https://github.com/dan-da/highlight.php)[ Packagist](https://packagist.org/packages/dan-da/highlight.php)[ RSS](/packages/dan-da-highlightphp/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (33)Used By (0)

highlight.php
=============

[](#highlightphp)

*highlight.php* is a server side code highlighter written in PHP that currently supports 185 languages. It's a port of [highlight.js](http://www.highlightjs.org) by Ivan Sagalaev that makes full use of the language and style definitions of the original JavaScript project.

The original highlight.js and highlight.php implement highlighting by means of css classes, for use in html.

[geekdevs/highlight.php](https://github.com/geekdevs/highlight.php) is a fork that supports using ansi escape sequences for CLI colors in addition to CSS classes, but it does not (at time of writing) define a useful set of default colors.

dan-da/highlight.php is a fork that implements a (partial) default set of CLI colors and really only cares about that.

If you do not need CLI (ansi) colors, you should stick with one of the other forks.

Installation + Setup
--------------------

[](#installation--setup)

The recommended approach is to install the project through [Composer](https://getcomposer.org/).

```
composer require dan-da/highlight.php
```

If you're not using Composer, ensure that the classes defined in the `Highlight` namespace can be found either by inclusion or by an autoloader. A trivial autoloader for this purpose is included in this project as `Highlight\Autoloader.php`

Usage
-----

[](#usage)

### CLI Highlighting

[](#cli-highlighting)

```
    $jsonbuf = json_encode( ['age': 10, 'weight': 90] );
    $highlighter = new \Highlight\Highlighter(
            new \Highlight\Decorator\StatefulCliDecorator([])
    );
    echo $highlighter->highlight('json', $jsonbuf)->value;

```

The `Highlight\Highlighter` class contains the highlighting functionality. You can choose between two highlighting modes:

1. explicit mode
2. automatic language detection mode

### Explicit Mode

[](#explicit-mode)

In explicit mode, you must define which language you will be highlighting as.

```
// Instantiate the Highlighter.
$hl = new Highlight\Highlighter();
$code = file_get_contents('some_ruby_script.rb');

try {
    // Highlight some code.
    $highlighted = $hl->highlight('ruby', $code);

    echo "\"hljs {$highlighted->language}\">\n";
    echo $highlighted->value . "\n";
    echo "\n";
}
catch (DomainException $e) {
    // This is thrown if the specified language does not exist

    echo "\n";
    echo $code . "\n";
    echo "\n";
}
```

### Automatic Language Detection Mode

[](#automatic-language-detection-mode)

Alternatively you can use the automatic detection mode, which highlights your code with the language the library thinks is best.

> **Warning:** You must supply a list of languages that the `Highlighter` will pick from. This occurs in a brute force fashion and the language with the most accurate result will be selected. This is extremely inefficient as you supply more languages and may not always be 100% accurate.
>
> It is highly recommended you explicitly choose the language or limit the number of languages to automatically detect to reduce the number of inaccuracies.

```
$hl = new Highlight\Highlighter();
$hl->setAutodetectLanguages(array('ruby', 'python', 'perl'));

$highlighted = $hl->highlightAuto(file_get_contents('some_ruby_script.rb'));

echo "\"hljs {$highlighted->language}\">\n";
echo $highlighted->value . "\n";
echo "\n";
```

### Stylesheets

[](#stylesheets)

The same stylesheets available in the **highlight.js** project are available in the `styles` directory of this project and may be included in your own CSS or made accessible to your web server.

Versioning
----------

[](#versioning)

This project will follow the same version numbers as the highlight.js project with regards to languages, meaning that a language definition available in highlight.js 9.12.0 will be available in highlight.php 9.12.0. However, there are times where bugs may arise in this project or its translated definition files, so there'll be one more number appended to the version number. For example, version 9.12.0.1 will contain all of the same languages as highlight.js 9.12.0 but also contain fixes solely to this project. This is done so this project can have version bumps without conflicts should highlight.js release version 9.12.1.

### Backward Compatibility Promise

[](#backward-compatibility-promise)

Despite the fact that the semantic versioning used in this project mirrors that of highlight.js, this project will adhere to [Symfony's Backward Compatibility Promise](https://symfony.com/doc/current/contributing/code/bc.html#using-symfony-code). You can rest assured that there will be no breaking changes during `9.x` and any deprecations will be marked with `@deprecated` and won't be removed until the next major release.

Some History
------------

[](#some-history)

Dan Libby June 26, 2019

Forked from geekdevs/highlight.php to add default CLI color scheme.

Geert Bergman Sep 30, 2013

JavaScript code highlighting is very convenient and in many cases just what you want to use. Especially for programming blogs I would not advice you to use otherwise. But there are occasions where you're better off with a more 'static' approach, for instance if you want to send highlighted code in an email or for API documents. For this I needed a code highlighting program preferably written in PHP.

I couldn't found any satisfactory PHP solution so I decided to port one from JavaScript. After some comparison of different highlighting programs based on license, technology, language support [highlight.js](http://www.highlightjs.org) came out most favorable in my opinion.

It was my decision not to make a PHP highlighter but to do a port of highlight.js, these are different things. The goal was to make it work exactly as [highlight.js](http://www.highlightjs.org) to make as much use as possible of the language definitions and CSS files of the original program.

Happy coding!

LICENSE
-------

[](#license)

[BSD](./LICENSE.md)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~67 days

Recently: every ~28 days

Total

29

Last Release

2518d ago

Major Versions

v7.5 → v8.02014-05-03

v8.9.1 → v9.0.02016-01-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/e87d6b2a83a8e59034011ac8752ebe495e09793f4baa9ace68a1329a459c3aae?d=identicon)[danda](/maintainers/danda)

---

Top Contributors

[![allejo](https://avatars.githubusercontent.com/u/1246453?v=4)](https://github.com/allejo "allejo (77 commits)")[![scrivo](https://avatars.githubusercontent.com/u/5256809?v=4)](https://github.com/scrivo "scrivo (52 commits)")[![dan-da](https://avatars.githubusercontent.com/u/5110592?v=4)](https://github.com/dan-da "dan-da (9 commits)")[![azaghal](https://avatars.githubusercontent.com/u/809269?v=4)](https://github.com/azaghal "azaghal (4 commits)")[![jenbuzz](https://avatars.githubusercontent.com/u/3244222?v=4)](https://github.com/jenbuzz "jenbuzz (4 commits)")[![geekdevs](https://avatars.githubusercontent.com/u/864822?v=4)](https://github.com/geekdevs "geekdevs (3 commits)")[![assertchris](https://avatars.githubusercontent.com/u/200609?v=4)](https://github.com/assertchris "assertchris (3 commits)")[![S1SYPHOS](https://avatars.githubusercontent.com/u/12161504?v=4)](https://github.com/S1SYPHOS "S1SYPHOS (3 commits)")[![JonasDoebertin](https://avatars.githubusercontent.com/u/1367270?v=4)](https://github.com/JonasDoebertin "JonasDoebertin (1 commits)")[![pana1990](https://avatars.githubusercontent.com/u/6630197?v=4)](https://github.com/pana1990 "pana1990 (1 commits)")[![cebe](https://avatars.githubusercontent.com/u/189796?v=4)](https://github.com/cebe "cebe (1 commits)")

---

Tags

codehighlightsyntaxhighlight.jshighlight.php

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dan-da-highlightphp/health.svg)

```
[![Health](https://phpackages.com/badges/dan-da-highlightphp/health.svg)](https://phpackages.com/packages/dan-da-highlightphp)
```

###  Alternatives

[scrivo/highlight.php

Server side syntax highlighter that supports 185 languages. It's a PHP port of highlight.js

71140.3M82](/packages/scrivo-highlightphp)[endroid/qr-code

Endroid QR Code

4.8k67.6M348](/packages/endroid-qr-code)[laminas/laminas-code

Extensions to the PHP Reflection API, static code scanning, and code generation

1.9k185.4M172](/packages/laminas-laminas-code)[nette/php-generator

🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.5 features.

2.3k64.2M576](/packages/nette-php-generator)[jetbrains/phpstorm-stubs

PHP runtime &amp; extensions header files for PhpStorm

1.4k27.7M68](/packages/jetbrains-phpstorm-stubs)[easybook/geshi

GeSHi - Generic Syntax Highlighter. This is an unmodified port of GeSHi project code found on SourceForge.

211.0M10](/packages/easybook-geshi)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
