1. Go to this page and download the library: Download fleshgrinder/format 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/ */
fleshgrinder / format example snippets
use Fleshgrinder\Core\Formatter;
assert(Formatter::format('Hello, {}!', ['World']) === 'Hello, World!');
assert(Formatter::format('The number is {}', [1000]) === 'The number is 1,000');
assert(Formatter::format('{}', [[1, 2, 3]]) === '1, 2, 3');
assert(Formatter::format('{value}', ['value' => 42]) === '42');
assert(Formatter::format('{} {}', [1, 2]) === '1 2');
assert(Formatter::format('{.3}', [0.123456789]) === '0.123');
assert(Formatter::format('{.2:and}', [[1, 2, 3]]) === '1.00, 2.00, and 3.00');
assert(Formatter::format('{#b}', [2]) === '0b10');
assert(Formatter::format('{:?}', [tmpfile()]) === 'stream resource');
use Fleshgrinder\Core\Formatter;
try {
$expected = ['a', 'b', 'c'];
$actual = 'x';
if (in_array($actual, $expected, true) === false) {
throw new InvalidArgumentException(Formatter::format(
'Value must be one of {expected:or}, got {actual}',
['expected' => $expected, 'actual' => $actual]
));
}
}
catch (InvalidArgumentException $e) {
assert($e->getMessage() === 'Value must be one of a, b, or c, got x');
}
use Fleshgrinder\Core\Formatter;
assert(Formatter::format('{.1:and}', [[0, 1, 2]]) === '0.0, 1.0, and 2.0');
assert(Formatter::format('{#b:and}', [[0, 1, 2]]) === '0b0, 0b1, and 0b10');
assert(Formatter::format('{#o:and}', [[7, 8, 9]]) === '0o7, 0o10, and 0o11');
assert(Formatter::format('{#x:and}', [[9, 10, 11]]) === '0x9, 0xA, and 0xB');
use Fleshgrinder\Core\Formatter;
$pattern = 'This is always printed[, and this is printed only if {this_argument}? is non-empty]';
assert(
Formatter::format($pattern, ['this_argument' => ''])
=== 'This is always printed'
);
assert(
Formatter::format($pattern, ['this_argument' => 'this argument'])
=== 'This is always printed, and this is printed only if this argument is non-empty'
);
> use Fleshgrinder\Core\Formatter;
>
> assert(Formatter::format('[{}?]', ['foobar']) === '');
>