PHP Code Syntax Highlighter for CLI

0.4.0 2020-01-08 07:41 UTC

This package is auto-updated.

Last update: 2021-11-08 11:51:26 UTC


Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License

        _   _          _
  ___  | | (_)  ___   | |__
 / __| | | | | / __|  '  _  \
| (__  | | | | \___ \ | | | |
 \___| |_| |_| |____/ |_| |_|

PHP CLI Syntax Highlight Tool


As phar binary

curl -SsLo ~/clish.phar

chmod +x ~/clish.phar && sudo ln -s ~/clish.phar /usr/local/bin/clish

Follow same steps to upgrade.

As standalone binary

composer global require adhocore/cli-syntax

Follow same steps to upgrade.

As project dependency

composer require adhocore/cli-syntax


Shell command

If you installed as binary following any of the above methods, then:

# you will be able to run it as
clish -h
clish -f file.php
echo '<?php date("Ymd");' | clish
cat file.php | clish

# export png
clish -f file.php -o file.png

clish stands for CLI syntax highlight.


Parameter options:

  [-e|--echo]            Forces echo to STDOUT when --output is passed
  [-f|--file]            Input PHP file to highlight and/or export
                         (will read from piped input if file not given)
  [-F|--font]            Font to use for export to png
  [-l|--with-line-no]    Highlight with line number
  [-o|--output]          Output filepath where PNG image is exported

Run clish -h to show help.

  bin/clish --file file.php                           # print
  cat file.php | bin/clish                            # from piped stream
  bin/clish < file.php                                # from redirected stdin
  bin/clish --file file.php --output file.png         # export
  bin/clish --file file.php --output file.png --echo  # print + export
  bin/clish --file file.php --with-line-no            # print with lineno
  bin/clish -f file.php -o file.png -F dejavu         # export in dejavu font


You can either highlight PHP code in terminal output or export to png image.


use Ahc\CliSyntax\Highlighter;

// PHP code
echo new Highlighter('<?php echo "Hello world!";');
// OR
echo (new Highlighter)->highlight('<?php echo "Hello world!";', $options);

// PHP file
echo Highlighter::for('/path/to/file.php', $options);

// $options array is optional and can contain:
    'lineNo' => true, // bool


use Ahc\CliSyntax\Exporter;

// PHP file
Exporter::for('/path/to/file.php')->export('file.png', $options);

// $options array is optional and can contain:
    'lineNo' => true, // bool
    'font'   => 'full/path/of/font.ttf', // str
    'size'   => 'font size', // int

See example usage. Here's how the export looks like:


And with line numbers:

Example with line numbers


If you would like to change color etc, extend the classes Highlighter and Exporter, then override visit() method which recieves DOMNode.


Please check the guide.


© MIT | 2019, Jitendra Adhikari


This project is bootstrapped by phint and releases managed by please.