PHPackages                             geekdevs/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. geekdevs/highlight.php

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

geekdevs/highlight.php
======================

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

v9.15.6.1(7y ago)03661[3 issues](https://github.com/geekdevs/highlight.php/issues)[3 PRs](https://github.com/geekdevs/highlight.php/pulls)1BSD-3-ClausePHP

Since Apr 29Pushed 6y ago1 watchersCompare

[ Source](https://github.com/geekdevs/highlight.php)[ Packagist](https://packagist.org/packages/geekdevs/highlight.php)[ RSS](/packages/geekdevs-highlightphp/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (2)Versions (29)Used By (1)

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

[](#highlightphp)

[![Build Status](https://camo.githubusercontent.com/f19634bf4b1416fa5e310d61b5fa755c67e5f61e541ec02b774a58cca398a110/68747470733a2f2f7472617669732d63692e6f72672f73637269766f2f686967686c696768742e7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/scrivo/highlight.php)[![Latest Packagist release](https://camo.githubusercontent.com/20f0ce5694f9cea22a9b071591751bfad7f15835cd2848efbced4e32573f5474/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73637269766f2f686967686c696768742e7068702e737667)](https://packagist.org/packages/scrivo/highlight.php)[![Monthly downloads on Packagist](https://camo.githubusercontent.com/f38a689fb4444183e0442c588db18dabd75fdfb4bb9b9bf630d7bf08db3b44b0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f73637269766f2f686967686c696768742e7068702e737667)](https://packagist.org/packages/scrivo/highlight.php/stats)

*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.

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

[](#installation--setup)

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

```
composer require scrivo/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`

### Composer Version Constraints

[](#composer-version-constraints)

When requiring this project in your `composer.json`, it is recommended you use the [caret version range](https://getcomposer.org/doc/articles/versions.md#caret-version-range-) and use only the major and minor values; i.e. `^9.14`.

It's come to our attention that a lot of tutorials and projects out there are locking themselves into highly specific versions of this project; e.g. `"scrivo/highlight.php": "v9.12.0.1"`. Please do **not** do this or encourage it. We promise a [reliable backward compatibility policy](#backward-compatibility-promise) so there's no reason to lock yourself to such a specific version. By doing this, you are preventing yourself or your users from receiving updates to language definitions and bug fixes.

Usage
-----

[](#usage)

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)

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

Popularity13

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 51.7% of commits — single point of failure

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 ~66 days

Recently: every ~10 days

Total

28

Last Release

2614d ago

Major Versions

v7.5 → v8.02014-05-03

v8.9.1 → v9.0.02016-01-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/7c32f742614130f40944df419b067a2d308175dfcc1ea2ced80a37885585aa53?d=identicon)[geekdevs](/maintainers/geekdevs)

---

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)")[![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)")[![assertchris](https://avatars.githubusercontent.com/u/200609?v=4)](https://github.com/assertchris "assertchris (3 commits)")[![geekdevs](https://avatars.githubusercontent.com/u/864822?v=4)](https://github.com/geekdevs "geekdevs (3 commits)")[![S1SYPHOS](https://avatars.githubusercontent.com/u/12161504?v=4)](https://github.com/S1SYPHOS "S1SYPHOS (3 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)")[![JonasDoebertin](https://avatars.githubusercontent.com/u/1367270?v=4)](https://github.com/JonasDoebertin "JonasDoebertin (1 commits)")

---

Tags

codehighlightsyntaxhighlight.jshighlight.php

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[scrivo/highlight.php

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

71040.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.2k64.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)
