Output Customization

The default behaviour of Text_Highlighter is to generate a syntax highlighted HTML version of the input.

It is possible to instead generate output that is suitable for being displayed on color-capable terminals such as xterm or through less(1) by telling Text_Highlighter to use another renderer:

Using the console renderer

require_once "Text/Highlighter.php";

$hlSQL =& Text_Highlighter::factory("SQL");
$hlSQL->setRenderer(new Text_Highlighter_Renderer_Console);
$hlSQL->highlight("SELECT * FROM some_table WHERE id = 12");

Also it is possible to further customize the output of both the HTML- and the console-renderer by passing an associative array of options to the constructor:

Options for the HTML renderer

require_once "Text/Highlighter.php";

$renderer = new Text_Highlighter_Renderer_Html(array("numbers" => HL_NUMBERS_LI"tabsize" => 4));

$hlJava =& Text_Highlighter::factory("JAVA");

$hlJava->highlight('class JavaProgram {
    public static void main(String args[]) {
        System.out.println("Hello World!");

The above example configures a HTML renderer with two options: The first one tells it to number the lines in the output using the <ol /> HTML tag and the second one instructs the renderer to use a tab width of 4 spaces for indentation.

The following options are applicable:

Possible options for the renderer classes
Name Description Available in HTML renderer Available in console renderer Hints
numbers Line numbering style yes yes In the console renderer, this option takes just TRUE or FALSE in order to denote if line numbers should be displayed or not. The HTML rendered accepts three different values: HL_NUMBERS_LI instructs the class to number the lines using the <ol /> HTML tag, while HL_NUMBERS_TABLE uses a two-column table with the line numbers in the first and the source code in the second column. Setting the value to FALSE turns line numbering off in the HTML renderer.
tabsize Tab width yes yes  
colors Additional colors no yes An associate array of additional colors for color-enabled consoles. The key of each array entry must be the name of the color and the corresponding value must be the escape sequence for it. (Example: \033[1;31mRed.)
Usage (Previous) Text_LanguageDetect (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: Axeia
Note that this package uses css classesB for syntax highlighting but does not by default do anything to include a stylesheet with these classes defined.

This means that if you use the default options it will give back normal looking plain text, if you're using the line-numbering option it wont even be indented (uses a <ol> instead of <pre> to PREserve whitespace)

So in the HTML you're outputting it to you may want to include the sample.css stylesheet that's in the package.

e.g. in the <head> section of your file use
<link rel="stylesheet" type="text/css" href="sample.css" />
(if the sample.css file has been placed in the same directory as the file outputting the text - but that's basic HTML knowledge which I assume everyone interested in this is familiar with).

Going by the bugtracker a future release should allow different highlighting on a per language base by assigning the container element a class based on the used language.
.hl-java .hl-string{ color: purple; /*Only java gets purple colored strings.*/ }