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.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#
# 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
	else:
		result = blank_line_handler()
		print "Blank line found, handler returned: %s" % result

 

Features:

  • 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

Usage:

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.
derek@derek-laptop:~$

 

Then just pass in the appropriate arguments:

<?php

// 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!
$tabwidth=4

// 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'"
$extra_opts=""

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

<?php
	$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
	else:
		result = blank_line_handler()
		print "Blank line found, handler returned: %s" % result
EOD;
    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

Download from github: https://github.com/dereks/pygments_for_php

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.

Lexers:
~~~~~~~
* 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:
    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:
    Makefile
* bash, sh:
    Bash (filenames *.sh, *.ebuild, *.eclass)
* bat:
    Batchfile (filenames *.bat, *.cmd)
* bbcode:
    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+Django/Jinja
* css+erb, css+ruby:
    CSS+Ruby
* css+genshitext, css+genshi:
    CSS+Genshi Text
* css+mako:
    CSS+Mako
* css+mako:
    CSS+Mako
* css+myghty:
    CSS+Myghty
* css+php:
    CSS+PHP
* css+smarty:
    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:
    Django/Jinja
* dpatch:
    Darcs Patch (filenames *.dpatch, *.darcspatch)
* dylan:
    Dylan (filenames *.dylan)
* erb:
    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+Cheetah
* html+django, html+jinja:
    HTML+Django/Jinja
* html+evoque:
    HTML+Evoque (filenames *.html)
* html+genshi, html+kid:
    HTML+Genshi
* html+mako:
    HTML+Mako
* html+mako:
    HTML+Mako
* html+myghty:
    HTML+Myghty
* html+php:
    HTML+PHP (filenames *.phtml)
* html+smarty:
    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:
    JavaScript+Cheetah
* js+django, javascript+django, js+jinja, javascript+jinja:
    JavaScript+Django/Jinja
* js+erb, javascript+erb, js+ruby, javascript+ruby:
    JavaScript+Ruby
* js+genshitext, js+genshi, javascript+genshitext, javascript+genshi:
    JavaScript+Genshi Text
* js+mako, javascript+mako:
    JavaScript+Mako
* js+mako, javascript+mako:
    JavaScript+Mako
* js+myghty, javascript+myghty:
    JavaScript+Myghty
* js+php, javascript+php:
    JavaScript+PHP
* js+smarty, javascript+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:
    MySQL
* nasm:
    NASM (filenames *.asm, *.ASM)
* newspeak:
    Newspeak (filenames *.ns2)
* nginx:
    Nginx configuration file
* numpy:
    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:
    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)
* vb.net, vbnet:
    VB.net (filenames *.vb, *.bas)
* vim:
    VimL (filenames *.vim, .vimrc)
* xml+cheetah, xml+spitfire:
    XML+Cheetah
* xml+django, xml+jinja:
    XML+Django/Jinja
* xml+erb, xml+ruby:
    XML+Ruby
* xml+evoque:
    XML+Evoque (filenames *.xml)
* xml+mako:
    XML+Mako
* xml+mako:
    XML+Mako
* xml+myghty:
    XML+Myghty
* xml+php:
    XML+PHP
* xml+smarty:
    XML+Smarty
* xml:
    XML (filenames *.xml, *.xsl, *.rss, *.xslt, *.xsd, *.wsdl)
* xslt:
    XSLT (filenames *.xsl, *.xslt)
* yaml:
    YAML (filenames *.yaml, *.yml)

[...other docs snipped...]

Styles:
~~~~~~~
* 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.