PHP code example of orware / compressed-string

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

    

orware / compressed-string example snippets


use Orware\Compressed\CompressedString;

$compressedString = new CompressedString();

// You may write multiple times:
$content = 'The quick brown fox jumps over the lazy dog';
$compressedString->write($content);

$moreContent = 'The quick brown fox jumps over the lazy dog';
$compressedString->write($moreContent);

// You can prepend text as well
// (currently this involves creating a new stream, adding the prepended text, then copying the existing stream into the new stream):
$textToPrepend = 'PREPENDED';
$compressedString->prepend($textToPrepend);

// You can write more text after prepending text:
$evenMoreContent = 'The quick brown fox jumps over the lazy dog';
$compressedString->write($evenMoreContent);

// You can pass in a PHP array or object and it will get automatically JSON encoded:
$list = [1, 2, 3, 4, 5, 6];
$compressedString->write($list);

// Gets the Decompressed String:
$decompressedString = $compressedString->getDecompressedContents();

// Writes the Decompressed String Directly to a file:
$compressedString->writeDecompressedContents('tests/files/appended_test_decompressed.txt');

// Writes the Compressed String Directly to a file:
$compressedString->writeCompressedContents('tests/files/appended_test_compressed.gz');


use Orware\Compressed\CompressedString;
use Orware\Compressed\CompressedStringList;

$compressedString1 = new CompressedString();
$content = 'My first string';
$compressedString1->write($content);

$compressedString2 = new CompressedString();
$content = 'My second string';
$compressedString2->write($content);

$compressedString3 = new CompressedString();
$content = 'My third string';
$compressedString3->write($content);

// You must use this StringList class (it's what the merge call below expects):
$list = new CompressedStringList();

$list->enqueue($compressedString1);
$list->enqueue($compressedString2);
$list->enqueue($compressedString3);

// The default placeholder is #|_|#
// Each instance of that placeholder below will get replaced:
$subject = '{"string1":"#|_|#","string2":"#|_|#","string3":"#|_|#"}';

// The end result is a new compressed string.
// Depending on the size of your compressed strings execution
// time may go up during a merge since each has to be decoded
// and compressed again when merged into the new string.
$mergedString = CompressedStringList::merge($subject, '#|_|#', $list);

shell
curl -s http://getcomposer.org/installer | php
php composer.phar