PHP code example of dan-da / highlight.php

1. Go to this page and download the library: Download dan-da/highlight.php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dan-da / highlight.php example snippets


// Instantiate the Highlighter.
$hl = new Highlight\Highlighter();
$code = file_get_contents('some_ruby_script.rb');

try {
    // Highlight some code.
    $highlighted = $hl->highlight('ruby', $code);

    echo "<pre class=\"hljs {$highlighted->language}\">\n";
    echo $highlighted->value . "\n";
    echo "</pre>\n";
}
catch (DomainException $e) {
    // This is thrown if the specified language does not exist

    echo "<pre>\n";
    echo $code . "\n";
    echo "</pre>\n";
}

$hl = new Highlight\Highlighter();
$hl->setAutodetectLanguages(array('ruby', 'python', 'perl'));

$highlighted = $hl->highlightAuto(file_get_contents('some_ruby_script.rb'));

echo "<pre class=\"hljs {$highlighted->language}\">\n";
echo $highlighted->value . "\n";
echo "</pre>\n";
bash
composer 

    $jsonbuf = json_encode( ['age': 10, 'weight': 90] );
    $highlighter = new \Highlight\Highlighter(
            new \Highlight\Decorator\StatefulCliDecorator([])
    );
    echo $highlighter->highlight('json', $jsonbuf)->value;