A Syntax Highlighter written in PHP

0.3.1 2023-06-16 20:15 UTC

This package is auto-updated.

Last update: 2024-05-16 22:19:38 UTC


PHP from Packagist Codacy Badge Latest Stable Version Total Downloads License


A Syntax Highlighter written in PHP using RegEx. Bringing beautiful styles. Easy to extend.



This package is available for the use with Composer. Use composer to install by running $ composer require bitandblack/syntaxhighlighter and $ composer update.


Set up the highlighter like that:


use BitAndBlack\SyntaxHighlighter\Output\PHP;
use BitAndBlack\SyntaxHighlighter\SyntaxHighlighter;

$code = '<?php echo \'Hello world!\'; ?>';

$syntaxHighlighter = new SyntaxHighlighter(
    new PHP()

Print the syntaxed code then:

<pre><code class="default php">
<?php echo $syntaxHighlighter; ?>

You can print the code also by using $syntaxHighlighter->getSyntax().

Note that there's no need to convert the input with htmlspecialchars() or something else. Highlighter will do this by its own.

Using the dynamic language loader

If you want to use the dynamic language rule loader you need to add the following code before calling the SyntaxHighlighter class.


use BitAndBlack\SyntaxHighlighter\Factories\RulesLoadingFactory;
use BitAndBlack\SyntaxHighlighter\Loading\Manager\OutputLoaderManager;
use BitAndBlack\SyntaxHighlighter\SyntaxHighlighter;

require_once 'vendor/autoload.php';

// The first argument is the folder where the rules are saved
// The second argument is the factory to get the loader classes for the files in the rules folder
$manager = new OutputLoaderManager('src/Rules', new RulesLoadingFactory());

// Both methods will return null if there was no rule set found!
$fileBasedRuleSet = $manager->getOutputForFile('test.php');
$languageBasedRuleSet = $manager->getOutputForLanguage('php');

$syntaxHighlighter = new SyntaxHighlighter(
    //You can use the $fileBasedRuleSet rule set as well here!

There is only a rule for php at the moment, if you want to create rules on your own use the following json template to do so

    "extensions": [
    "languages": [
    "rules": {
        "number": [
        "constant": [
        "setget": [
        "variable": [
            "/((?<=-&gt;)[a-zA-Z0-9]+)(?=\\[| )/"
        "static": [
        "keyword": [
        "string": [
        "comment": [
        "docblock": [
        "default": [
            "/((?=use )([^;]*))/"
  • Values listed in the extensions key will list all the allowed files extensions for the rule set
  • Values listed in the languages key will define all the languages allowed to use with this rule set
  • Values listed below the rules key will define the rules the highlighter will use for highlighting


There's a different package holding some nice styles written in scss which can be used with NPM or Yarn. This makes it easier to add them to your project.

You can find the packe under

Writing a custom output

It's possible to write a custom output with custom regex rules. This class needs to implement the OutputInterface.


If you have any questions, feel free to contact us under

Further information about Bit&Black can be found under