1. Go to this page and download the library: Download mordilion/huffman-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/ */
mordilion / huffman-php example snippets
use Mordilion\HuffmanPHP\Dictionary;
use Mordilion\HuffmanPHP\Huffman;
// Build a dictionary from sample text
$text = 'This is a Text to compress with the Huffman-Algorithm'; // 53 chars
$dictionary = new Dictionary([$text]);
$huffman = new Huffman($dictionary);
// Encode to binary
$binary = $huffman->encode($text);
// => '10000010101000011011010000110110010101110000101100010111001111010100110001001010100100001100100101100110011011000010010011001010111100101101101100000100000000011000111001001011111111110111011110010001101000100010011001011001' (289 chars)
// Encode with compression (URL-safe Base65, default)
$compressed = $huffman->encode($text, true);
// => '3QGgsnulxJqC2QweIz-V6SWj~pYoqYfA005HCR' (38 chars from 53)
// Decode
$decoded = $huffman->decode($binary); // original text
$decoded = $huffman->decode($compressed, true); // original text
$huffman = new Huffman($dictionary, Huffman::ALPHABET_BASE26_LOWER);
$compressed = $huffman->encode($text, true);
// => 'mtxhycztntclyrustzsjioonyevmiijcdwrxqflkwsymxtsb' (48 chars)
$decoded = $huffman->decode($compressed, true); // original text
$text = 'A Text with multiple Text Segments, to demonstrate the compression with multiple Text Segments'; // 94 chars
// Split into words and add space as a separate token
$tokens = array_merge(array_unique(explode(' ', $text)), [' ']);
$dictionary = new Dictionary($tokens, Dictionary::MAX_LENGTH_WHOLE_WORDS);
$huffman = new Huffman($dictionary);
$compressed = $huffman->encode($text, true);
// => '27thCP8gJKOciDUU' (16 chars from 94)
$decoded = $huffman->decode($compressed, true); // original text
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.