PHP code example of spatie / shiki-php

1. Go to this page and download the library: Download spatie/shiki-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/ */

    

spatie / shiki-php example snippets


\Spatie\ShikiPhp\Shiki::highlight(
    code: ' echo "Hello World"; 

use Spatie\ShikiPhp\Shiki;

Shiki::highlight(
    code: ' echo "Hello World"; 

<pre class="shiki" style="background-color: #2e3440ff"><code><span class="line"><span style="color: #81A1C1">&lt;?</span><span style="color: #D8DEE9FF">php </span><span style="color: #81A1C1">echo</span><span style="color: #D8DEE9FF"> </span><span style="color: #ECEFF4">&quot;</span><span style="color: #A3BE8C">Hello World</span><span style="color: #ECEFF4">&quot;</span><span style="color: #81A1C1">;</span><span style="color: #D8DEE9FF"> </span><span style="color: #81A1C1">?&gt;</span></span></code></pre>

use Spatie\ShikiPhp\Shiki;

// Highlighting lines 1 and 4,5,6
Shiki::highlight(
    code: $code,
    language: 'php',
    highlightLines: [1, '4-6'],
);

// Marking line 1 as added
Shiki::highlight(
    code: $code,
    language: 'php',
    addLines: [1],
);

// Marking line 1 as deleted
Shiki::highlight(
    code: $code,
    language: 'php',
    deleteLines: [1],
);

// Marking line 1 as focus
Shiki::highlight(
    code: $code,
    language: 'php',
    focusLines: [1],
);

// As reference
highlight(
    string $code,
    ?string $language = 'php',
    ?string $theme = 'nord',
    ?array $highlightLines = [],
    ?array $addLines = [],
    ?array $deleteLines = [],
    ?array $focusLines = []
)

// Instead of PHP 8 syntax
Shiki::highlight(
    code: $code,
    language: 'php',
    deleteLines: [1],
);

// You need to follow PHP 7.4 syntax
Shiki::highlight(
    $code,
    'php',
    null,
    null,
    [1],
);

$shiki = new \Spatie\ShikiPhp\Shiki();

$shiki->getAvailableLanguages(); // returns an array
$shiki->languageIsAvailable('php'); // returns true
$shiki->languageIsAvailable('non-existing-language'); // returns false

$shiki = new \Spatie\ShikiPhp\Shiki();

$shiki->getAvailableThemes(); // returns an array
$shiki->themeIsAvailable('github-light'); // returns true
$shiki->themeIsAvailable('non-existing-theme'); // returns false

use Spatie\ShikiPhp\Shiki;

Shiki::highlight(
    code: ' echo "Hello World"; 
bash
composer