Pygments for PHP

This small library provides a PHP function called pygmentize(), which provides an easy interface to the pygmentize syntax highlighter command. Pygmentize is a command-line interface to the Pygments library, written in Python. It makes it easy to copy’n'paste raw code into your HTML+PHP documents.

print pygmentize( $some_python_code, $language="python", $style="monokai",
                  $tabwidth=4, $extra_opts="-O linenos=table,hl_lines='7 8 9'" );

# Here is some raw Python, pasted directly into the page:

# For every line...
for line in lines:
	# ...print it out if it's not blank:
	if line != "":
		print line
		result = blank_line_handler()
		print "Blank line found, handler returned: %s" % result



  • Supports tabs, with customizable tab width!
  • Copy’n'paste directly from the web page preserves tabs, too!
  • Server-side syntax highlighting. No Javascript, and no backend calls to 3rd-party servers.
  • Multiple different programming languages and styles per page.
  • Only a single PHP include_once() required. No need to link to CSS files.
  • Full Pygments support (197 languages, incl. combos like HTML+PHP, and 18 styles).
  • Using WordPress? Check out the Pygments for WordPress plug-in


First, make sure that Pygments is installed on your web server. The Ubuntu/Debian package name is python-pygments:

derek@derek-laptop:~$ sudo apt-get install python-pygments
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-pygments is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 30 not upgraded.


Then just pass in the appropriate arguments:


// A simple PHP string holding your raw code.  
// Read it in from a file, or a database, or a simple variable.  Whatevah.
$code = '# Look for string:\ngrep -i string sample.php';

// The list of ~197 supported languages can be seen by running "pygmentize -L"
$language = "bash";

// The list of 18 supported styles can be seen by running "pygmentize -L"
$style = "fruity";

// TABS!

// This string is passed directly on the command line.
// It should start with "-O ", as per the pygmentize docs.
//$extra_opts="-O linenos=table,linenostart=57,hl_lines='69'"

// Note that only the first three args are required.  We leave
// out $tabwidth and $extra_opts in this example:
$html = pygmentize( $code, $language, $style );

print $html;  // Print it to the page... or whatever.  It's just a string of HTML and CSS.


The above PHP code will print the following HTML:

# Look for string:
grep -i string sample.php

  • Tip: To embed raw, unformatted code directly into your PHP pages, use a PHP nowdoc followed by a call to pygmentize(). This allows you to copy’n'paste raw code and wrap it with just a couple lines of PHP for a pretty display.

    PHP nowdoc syntax is just like heredoc, but with single-quotes around the identifier (in this case, the ‘EOD’). Unlike heredoc, nowdoc doesn’t require you to escape $ characters from PHP.

    You can also use a regular ” or ‘ delimited string, however, that will require to escape any ” or ‘ characters that might show up in your code.

<h2>Some Sample HTML</h2>

<b>Here</b> you can put any HTML code that you like...

	$code = <<<'EOD'
# Here is some raw Python, pasted directly into the page:

# For every line...
for line in lines:
	# ...print it out if it's not blank:
	if line != "":
		print line
		result = blank_line_handler()
		print "Blank line found, handler returned: %s" % result
    print pygmentize( $code, $language="python", $style="monokai",
                      $tabwidth=4, $extra_opts="-O linenos=table,hl_lines='7 8 9'" );

After that, you <b>can put more HTML as normal</b>.



Download from github:

Finally, here’s a quick list of the styles and languages that are available:

derek@derek-laptop:~$ pygmentize -L
Pygments version 1.2.2, (c) 2006-2008 by Georg Brandl.

* Cucumber, cucumber, Gherkin, gherkin:
    Gherkin (filenames *.feature)
* abap:
    ABAP (filenames *.abap)
* antlr-as, antlr-actionscript:
    ANTLR With ActionScript Target (filenames *.G, *.g)
* antlr-cpp:
    ANTLR With CPP Target (filenames *.G, *.g)
* antlr-csharp, antlr-c#:
    ANTLR With C# Target (filenames *.G, *.g)
* antlr-java:
    ANTLR With Java Target (filenames *.G, *.g)
* antlr-objc:
    ANTLR With ObjectiveC Target (filenames *.G, *.g)
* antlr-perl:
    ANTLR With Perl Target (filenames *.G, *.g)
* antlr-python:
    ANTLR With Python Target (filenames *.G, *.g)
* antlr-ruby, antlr-rb:
    ANTLR With Ruby Target (filenames *.G, *.g)
* antlr:
* apacheconf, aconf, apache:
    ApacheConf (filenames .htaccess, apache.conf, apache2.conf)
* applescript:
    AppleScript (filenames *.applescript)
* as, actionscript:
    ActionScript (filenames *.as)
* as3, actionscript3:
    ActionScript 3 (filenames *.as)
* aspx-cs:
    aspx-cs (filenames *.aspx, *.asax, *.ascx, *.ashx, *.asmx, *.axd)
* aspx-vb:
    aspx-vb (filenames *.aspx, *.asax, *.ascx, *.ashx, *.asmx, *.axd)
* asy:
    Asymptote (filenames *.asy)
* basemake:
* bash, sh:
    Bash (filenames *.sh, *.ebuild, *.eclass)
* bat:
    Batchfile (filenames *.bat, *.cmd)
* bbcode:
* befunge:
    Befunge (filenames *.befunge)
* boo:
    Boo (filenames *.boo)
* brainfuck, bf:
    Brainfuck (filenames *.bf, *.b)
* c-objdump:
    c-objdump (filenames *.c-objdump)
* c:
    C (filenames *.c, *.h)
* cheetah, spitfire:
    Cheetah (filenames *.tmpl, *.spt)
* clojure, clj:
    Clojure (filenames *.clj)
* cmake:
    CMake (filenames *.cmake)
* common-lisp, cl:
    Common Lisp (filenames *.cl, *.lisp, *.el)
* console:
    Bash Session (filenames *.sh-session)
* control:
    Debian Control file (filenames control)
* cpp, c++:
    C++ (filenames *.cpp, *.hpp, *.c++, *.h++, *.cc, *.hh, *.cxx, *.hxx)
* cpp-objdump, c++-objdumb, cxx-objdump:
    cpp-objdump (filenames *.cpp-objdump, *.c++-objdump, *.cxx-objdump)
* csharp, c#:
    C# (filenames *.cs)
* css+django, css+jinja:
* css+erb, css+ruby:
* css+genshitext, css+genshi:
    CSS+Genshi Text
* css+mako:
* css+mako:
* css+myghty:
* css+php:
* css+smarty:
* css:
    CSS (filenames *.css)
* cython, pyx:
    Cython (filenames *.pyx, *.pxd, *.pxi)
* d-objdump:
    d-objdump (filenames *.d-objdump)
* d:
    D (filenames *.d, *.di)
* delphi, pas, pascal, objectpascal:
    Delphi (filenames *.pas)
* diff, udiff:
    Diff (filenames *.diff, *.patch)
* django, jinja:
* dpatch:
    Darcs Patch (filenames *.dpatch, *.darcspatch)
* dylan:
    Dylan (filenames *.dylan)
* erb:
* erl:
    Erlang erl session (filenames *.erl-sh)
* erlang:
    Erlang (filenames *.erl, *.hrl)
* evoque:
    Evoque (filenames *.evoque)
* fortran:
    Fortran (filenames *.f, *.f90)
* gas:
    GAS (filenames *.s, *.S)
* genshi, kid, xml+genshi, xml+kid:
    Genshi (filenames *.kid)
* genshitext:
    Genshi Text
* glsl:
    GLSL (filenames *.vert, *.frag, *.geo)
* gnuplot:
    Gnuplot (filenames *.plot, *.plt)
* go:
    Go (filenames *.go)
* groff, nroff, man:
    Groff (filenames *.[1234567], *.man)
* haskell, hs:
    Haskell (filenames *.hs)
* html+cheetah, html+spitfire:
* html+django, html+jinja:
* html+evoque:
    HTML+Evoque (filenames *.html)
* html+genshi, html+kid:
* html+mako:
* html+mako:
* html+myghty:
* html+php:
    HTML+PHP (filenames *.phtml)
* html+smarty:
* html:
    HTML (filenames *.html, *.htm, *.xhtml, *.xslt)
* ini, cfg:
    INI (filenames *.ini, *.cfg, *.properties)
* io:
    Io (filenames *.io)
* irc:
    IRC logs (filenames *.weechatlog)
* java:
    Java (filenames *.java)
* js+cheetah, javascript+cheetah, js+spitfire, javascript+spitfire:
* js+django, javascript+django, js+jinja, javascript+jinja:
* js+erb, javascript+erb, js+ruby, javascript+ruby:
* js+genshitext, js+genshi, javascript+genshitext, javascript+genshi:
    JavaScript+Genshi Text
* js+mako, javascript+mako:
* js+mako, javascript+mako:
* js+myghty, javascript+myghty:
* js+php, javascript+php:
* js+smarty, javascript+smarty:
* js, javascript:
    JavaScript (filenames *.js)
* jsp:
    Java Server Page (filenames *.jsp)
* lhs, literate-haskell:
    Literate Haskell (filenames *.lhs)
* lighty, lighttpd:
    Lighttpd configuration file
* llvm:
    LLVM (filenames *.ll)
* logtalk:
    Logtalk (filenames *.lgt)
* lua:
    Lua (filenames *.lua)
* make, makefile, mf, bsdmake:
    Makefile (filenames *.mak, Makefile, makefile, Makefile.*, GNUmakefile)
* mako:
    Mako (filenames *.mao)
* mako:
    Mako (filenames *.mao)
* matlab, octave:
    Matlab (filenames *.m)
* matlabsession:
    Matlab session
* minid:
    MiniD (filenames *.md)
* modelica:
    Modelica (filenames *.mo)
* moocode:
    MOOCode (filenames *.moo)
* mupad:
    MuPAD (filenames *.mu)
* mxml:
    MXML (filenames *.mxml)
* myghty:
    Myghty (filenames *.myt, autodelegate)
* mysql:
* nasm:
    NASM (filenames *.asm, *.ASM)
* newspeak:
    Newspeak (filenames *.ns2)
* nginx:
    Nginx configuration file
* numpy:
* objdump:
    objdump (filenames *.objdump)
* objective-c, objectivec, obj-c, objc:
    Objective-C (filenames *.m)
* ocaml:
    OCaml (filenames *.ml, *.mli, *.mll, *.mly)
* ooc:
    Ooc (filenames *.ooc)
* perl, pl:
    Perl (filenames *.pl, *.pm)
* php, php3, php4, php5:
    PHP (filenames *.php, *.php[345])
* pot, po:
    Gettext Catalog (filenames *.pot, *.po)
* pov:
    POVRay (filenames *.pov, *.inc)
* prolog:
    Prolog (filenames *.prolog, *.pro, *.pl)
* py3tb:
    Python 3.0 Traceback (filenames *.py3tb)
* pycon:
    Python console session
* pytb:
    Python Traceback (filenames *.pytb)
* python, py:
    Python (filenames *.py, *.pyw, *.sc, SConstruct, SConscript)
* python3, py3:
    Python 3
* ragel-c:
    Ragel in C Host (filenames *.rl)
* ragel-cpp:
    Ragel in CPP Host (filenames *.rl)
* ragel-d:
    Ragel in D Host (filenames *.rl)
* ragel-em:
    Embedded Ragel (filenames *.rl)
* ragel-java:
    Ragel in Java Host (filenames *.rl)
* ragel-objc:
    Ragel in Objective C Host (filenames *.rl)
* ragel-ruby, ragel-rb:
    Ragel in Ruby Host (filenames *.rl)
* ragel:
* raw:
    Raw token data
* rb, ruby:
    Ruby (filenames *.rb, *.rbw, Rakefile, *.rake, *.gemspec, *.rbx)
* rbcon, irb:
    Ruby irb session
* rebol:
    REBOL (filenames *.r, *.r3)
* redcode:
    Redcode (filenames *.cw)
* rhtml, html+erb, html+ruby:
    RHTML (filenames *.rhtml)
* rst, rest, restructuredtext:
    reStructuredText (filenames *.rst, *.rest)
* scala:
    Scala (filenames *.scala)
* scheme, scm:
    Scheme (filenames *.scm)
* smalltalk, squeak:
    Smalltalk (filenames *.st)
* smarty:
    Smarty (filenames *.tpl)
* sourceslist, sources.list:
    Debian Sourcelist (filenames sources.list)
* splus, s, r:
    S (filenames *.S, *.R)
* sql:
    SQL (filenames *.sql)
* sqlite3:
    sqlite3con (filenames *.sqlite3-console)
* squidconf, squid.conf, squid:
    SquidConf (filenames squid.conf)
* tcl:
    Tcl (filenames *.tcl)
* tcsh, csh:
    Tcsh (filenames *.tcsh, *.csh)
* tex, latex:
    TeX (filenames *.tex, *.aux, *.toc)
* text:
    Text only (filenames *.txt)
* trac-wiki, moin:
    MoinMoin/Trac Wiki markup
* vala, vapi:
    Vala (filenames *.vala, *.vapi)
*, vbnet: (filenames *.vb, *.bas)
* vim:
    VimL (filenames *.vim, .vimrc)
* xml+cheetah, xml+spitfire:
* xml+django, xml+jinja:
* xml+erb, xml+ruby:
* xml+evoque:
    XML+Evoque (filenames *.xml)
* xml+mako:
* xml+mako:
* xml+myghty:
* xml+php:
* xml+smarty:
* xml:
    XML (filenames *.xml, *.xsl, *.rss, *.xslt, *.xsd, *.wsdl)
* xslt:
    XSLT (filenames *.xsl, *.xslt)
* yaml:
    YAML (filenames *.yaml, *.yml)

[...other docs snipped...]

* monokai:
    This style mimics the Monokai color scheme.
* manni:
    A colorful style, inspired by the terminal highlighting style.
* perldoc:
    Style similar to the style used in the perldoc code blocks.
* borland:
    Style similar to the style used in the borland IDEs.
* colorful:
    A colorful style, inspired by CodeRay.
* default:
    The default style (inspired by Emacs 22).
* murphy:
    Murphy's style from CodeRay.
* vs:

* trac:
    Port of the default trac highlighter design.
* tango:
    The Crunchy default Style inspired from the color palette from the Tango Icon Theme Guidelines.
* fruity:
    Pygments version of the "native" vim theme.
* autumn:
    A colorful style, inspired by the terminal highlighting style.
* bw:

* emacs:
    The default style (inspired by Emacs 22).
* pastie:
    Style similar to the pastie default style.
* friendly:
    A modern style based on the VIM pyte theme.
* native:
    Pygments version of the "native" vim theme.

Comments are closed.