DEV Community

Jitendra
Jitendra

Posted on

PHP Code Syntax highlighting (pretty printing) for CLI.

Introducing a small library I wrote over the weekend which prints syntax highlighted (colored) PHP code into the terminal.

You can use it in any PHP CLI applications to pretty print PHP code OR to take screenshot of code and use somewhere.

Right now screenshot is manual process but it will soon be automated into this library so you can save a PNG image of your code.

Installation

$ composer require adhocore/cli-syntax

Usage

use Ahc\CliSyntax\Highlighter;

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

// PHP file
echo Highlighter::for('/path/to/file.php');
Enter fullscreen mode Exit fullscreen mode

GitHub logo adhocore / php-cli-syntax

PHP Code Syntax Highlighter and/or exporter for CLI. Zero Dependency.

adhocore/cli-syntax

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License

        _   _          _
  ___  | | (_)  ___   | |__
 / __| | | | | / __|  '  _  \
| (__  | | | | \___ \ | | | |
 \___| |_| |_| |____/ |_| |_|
PHP CLI Syntax Highlight Tool
=============================

Installation

As phar binary

curl -SsLo ~/clish.phar https://github.com/adhocore/php-cli-syntax/releases/latest/download/clish.phar

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

Follow same steps to upgrade.

As standalone binary

composer global require adhocore/cli-syntax
Enter fullscreen mode Exit fullscreen mode

Follow same steps to upgrade.

As project dependency

composer require adhocore/cli-syntax
Enter fullscreen mode Exit fullscreen mode

Usage

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
Enter fullscreen mode Exit fullscreen mode

clish stands for CLI syntax highlight.

Options

Parameter options:

  [-e|--echo]            Forces echo to

Feedbacks and contributions are welcome. Thank you.

Top comments (1)

Collapse
 
vlasales profile image
Vlastimil Pospichal

I use Vim syntax highlighter.