A PHP wrapper for Pygments, the Python syntax highlighter, forked from kzykhys/pygments.

1.0.1 2017-03-28 00:58 UTC

This package is auto-updated.

Last update: 2020-10-30 01:44:20 UTC


Source Code Latest Version Software License Build Status Coverage Status Total Downloads

ramsey/pygments is a PHP wrapper for Pygments, the Python syntax highlighter, forked from the Pygments.php project.

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.


  • PHP 5.5.9 or greater (including PHP 7)
  • Python
  • Pygments (pip install Pygments)

Python and Pygments versions supported:

Pygments: 1.6 2.0 2.1 2.2
Python 2.6 -
Python 2.7
Python 3.2 - - -
Python 3.3
Python 3.4
Python 3.5
Python 3.6


The preferred method of installation is via Packagist and Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require ramsey/pygments


Highlight source code

use Ramsey\Pygments\Pygments;

$pygments = new Pygments();
$html = $pygments->highlight(file_get_contents('index.php'), 'php', 'html');
$text = $pygments->highlight('package main', 'go', 'ansi');

Generate CSS

use Ramsey\Pygments\Pygments;

$pygments = new Pygments();
$css = $pygments->getCss('monokai');
$prefixedCss = $pygments->getCss('default', '.syntax');

Guesses lexer name

use Ramsey\Pygments\Pygments;

$pygments = new Pygments();
$pygments->guessLexer('foo.rb'); // ruby

Get a list of lexers/formatters/styles

use Ramsey\Pygments\Pygments;

$pygments = new Pygments();

Set a custom pygmentize path

use Ramsey\Pygments\Pygments;

$pygments = new Pygments('/path/to/pygmentize');

Copyright and license

Copyrights for portions of ramsey/pygments are held by Kazuyuki Hayashi as part of Pygments.php. All other copyrights for ramsey/pygments are held by Ben Ramsey. Please see LICENSE for more information.