PHPH

PHP syntax highlighter by Martin Janecke

PHPH is a text-processing module for Aneamal which applies syntax highlighting to PHP code. It is actually a wrapper for PHP’s own highlight_string function. The text supplied to this module may contain content such as HTML tags that is not PHP code. PHP code must be preceded by <?php to be recognized and highlighted.

Examples

The first example applies syntax highlighting to the code from a linked PHP file:

[t-phph]->roman.php
<?php

// Roman numerals ordered by their value, descending
const numerals =
[
    
'M'  => 1000'CM' => 900,
    
'D'  => 500,  'CD' => 400,
    
'C'  => 100,  'XC' => 90,
    
'L'  => 50,   'XL' => 40,
    
'X'  => 10,   'IX' => 9,
    
'V'  => 5,    'IV' => 4,
    
'I'  => 1,
];

// converts a $number to its Roman representation
function roman (int $number): string
{
    if (
$number or $number 3999):
        throw new 
ValueError ('number outside range (1, 3999)');
    endif;

    
$string '';
    foreach (
numerals as $numeral => $value):
        while (
$number >= $value):
            
$string .= $numeral;
            
$number -= $value;
        endwhile;
    endforeach;
    return 
$string;
}

The second example highlights PHP code within a short embedded file that also contains plain text and HTML tags:

[t-phph]
|Accessed:
|<time><?php echo gmdate ('c'); ?></time>
Accessed:
<time><?php echo gmdate ('c'); ?></time>

License

PHPH is published under the GNU All-Permissive License:

Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty.

Installation

  1. Download PHPH 1.0.1.
  2. Unzip the downloaded ZIP archive, revealing the folder t-phph.
  3. Upload the folder t-phph into your aneamal directory.

Styling

The highlighted PHP code gets wrapped in a HTML div element with a class attribute whose value is the name of the module folder. That is t-phph unless you rename the folder:

<div class='t-phph'>
…
</div>

So you can use .t-phph as selector in stylesheets.

The HTML used for syntax highlighting inside the div element is generated by the highlight_string function of your server’s PHP installation and subject to change from one PHP version to the next even without this module getting an update.

Colors applied by PHP’s highlight_string function are set via php.ini directives. Webmasters can change these settings, but that is beyond the scope of this page.

Contact

Please write an email in case you found a bug or need help. You can find the developer’s email address here.

Version history

PHPH 1.0.1

  • simplified source code
  • published 2023-12-03

PHPH 1

  • initial release
  • works with Aneamal 30+
  • published 2023-11-29