PHP code example of fi1a / format
1. Go to this page and download the library: Download fi1a/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/ */
fi1a / format example snippets
use Fi1a\Format\Formatter;
Formatter::format('[{{user:login}}] - {{user:name}}', ['user' => ['name' => 'John', 'login' => 'john85']]); // [john85] - John
Formatter::format('{{0}}, {{1}}',[1, 2]); // 1, 2
Formatter::format('{{}}, {{}}',[1, 2]); // 1, 2
Formatter::format('{{foo}}, {{foo}}, {{foo}}', ['foo' => 'bar',]); // bar, bar, bar
use Fi1a\Format\Formatter;
use Fi1a\Format\AST\Exception\NotFoundKey;
try {
Formatter::format('{{not_exists}}', []);
} catch (NotFoundKey $exception) {
}
use Fi1a\Format\Formatter;
Formatter::format('{{value}}', ['value' => '&']); // "&"
Formatter::format('{{value|unescape}}', ['value' => '&']); // "&"
// или
Formatter::format('{{value}}', ['value' => '&'], [], false); // "&"
use Fi1a\Format\Formatter;
Formatter::format('[{{user:login}}] - {{user:name}}', ['user' => ['name' => 'John', 'login' => 'john85']]); // [john85] - John
use Fi1a\Format\Formatter;
Formatter::format('{{0}}, {{1}}',[1, 2]); // 1, 2
Formatter::format('{{}}, {{}}',[3, 4]); // 3, 4
use Fi1a\Format\Formatter;
Formatter::format('\\{{0\\}}', [0 => 'foo']); // {{0}}
Formatter::format('{{0}}', [0 => 'foo']); // foo
use Fi1a\Format\Formatter;
use Fi1a\Format\Safe;
Formatter::format(Safe::escape(('{{0}}'), [0 => 'foo']); // {{0}}
use Fi1a\Format\Formatter;
use Fi1a\Format\Safe;
Formatter::format(Safe::unescape(('\\{{0}}'), [0 => 'foo']); // foo
use Fi1a\Format\Formatter;
Formatter::format('{{0|sprintf("04d")}}-{{1|sprintf("02d")}}-{{2|sprintf("02d")}}',[2016, 2, 27,]); // 2016-02-27
use Fi1a\Format\Formatter;
Formatter::format('{{value|sprintf(modifier)}}', ['value' => 100.5], ['modifier' => '01.2f']); // 100.50
use Fi1a\Format\Formatter;
Formatter::format('{{value|unescape|sprintf|escape}}', ['value' => 'a&b']); // "a&b"
use Fi1a\Format\Formatter;
Formatter::format('{{0|sprintf("\'.9d")}}', [123]); // ......123
Formatter::format('{{0|sprintf("\'.09d")}}', [123]); // 000000123
use Fi1a\Format\Formatter;
Formatter::format('{{0|sprintf("04d")}}-{{1|sprintf("02d")}}-{{2|sprintf("02d")}}', [2020, 6, 7]); // 2020-06-07
use Fi1a\Format\Formatter;
Formatter::format('{{foo|date("d.m.Y")}}', ['foo' => time()]); // 28.09.2022
echo Formatter::format('{{|date("d F Y")}}', [time()]); // 18 Октября 2022
echo Formatter::format('{{|date("f")}}', [time()]); // Октябрь
use Fi1a\Format\Specifier\Date;
use Fi1a\Format\Formatter;
Formatter::format('{{foo|date}}', ['foo' => time()]); // 28.09.2022 07:06:00
Date::setDefaultFormat('d.m.Y');
Formatter::format('{{foo|date}}', ['foo' => time()]); // 28.09.2022
use Fi1a\Format\Formatter;
Formatter::format('{{|escape}}', ['"test"']); // "test"
use Fi1a\Format\Formatter;
Formatter::format('{{|unescape}}', ['&quot;test&quot;']); // "test"
use Fi1a\Format\Formatter;
Formatter::format('{{|memory}}', [1024]); // 1.0 КБ
Formatter::format('{{|memory("B")}}', [1024]); // 1024.0 Б
Formatter::format('{{|memory}}', [1024 * 1024]); // 1.0 МБ
use Fi1a\Format\Formatter;
Formatter::format('{{|time}}', [60 * 60]); // 1 ч.
Formatter::format('{{|time("minutes")}}', [60 * 60]); // 60 мин.
Formatter::format('{{|time}}', [2 * 24 * 60 * 60]); // 2 д.
use Fi1a\Format\Formatter;
Formatter::format('{{year}} {{year|declension("год", "года", "лет")}}', ['year' => 1]); // 1 год
Formatter::format('{{year}} {{year|declension("год", "года", "лет")}}', ['year' => 2]); // 2 года
Formatter::format('{{year}} {{year|declension("год", "года", "лет")}}', ['year' => 5]); // 5 лет
use Fi1a\Format\Formatter;
Formatter::format('{{value|phone("+7(ddd)ddd-dddd")}}', ['value' => '+79228223576']); // +7(922)822-3576
Formatter::format('{{value|phone("+7(ddd)ddd-dddd")}}', ['value' => '9228223576']); // +7(922)822-3576
Formatter::format('{{value|phone("(dddd)dd-dd-dd")}}', ['value' => '(6783)44-00-44']); // (6783)44-00-44
use Fi1a\Format\Formatter;
Formatter::format('{{value|number}}', ['value' => 100.00]); // 100
Formatter::format(
'{{value|number(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal)}}',
['value' => 100100.00],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => true,
]
); // 100 100.00
Formatter::format(
'{{value|number(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal)}}',
['value' => 100100.00],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
]
); // 100 100
Formatter::format(
'{{value|number(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal)}}',
['value' => 100100.12],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
]
); // 100 100.12
use Fi1a\Format\Formatter;
Formatter::addShortcut('number', 'number(2, ".", " ", false)');
Formatter::format('{{value|~number}}', ['value' => 100100.12]); // 100 100.12
Formatter::format('{{value|~number}}', ['value' => 100100]); // 100 100
use Fi1a\Format\Formatter;
Formatter::format('{{value|price}} руб.', ['value' => 100.00]); // 100 руб.
Formatter::format(
'{{value|price(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal)}} руб.',
['value' => 100100.00],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => true,
]
); // 100 100.00 руб.
Formatter::format(
'{{value|price(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal)}} руб.',
['value' => 100100.00],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
]
); // 100 100 руб.
Formatter::format(
'{{value|price(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal)}} руб.',
['value' => 100100.12],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
]
); // 100 100.12 руб.
Formatter::format(
'{{value|price(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal, round, roundPrecision)}} руб.',
['value' => 100100.5],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
'round' => PHP_ROUND_HALF_UP,
'roundPrecision' => 0,
]
); // 100 101 руб.
Formatter::format(
'{{value|price(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal, round, roundPrecision)}} руб.',
['value' => 100100.5],
[
'decimals' => 2,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
'round' => PHP_ROUND_HALF_DOWN,
'roundPrecision' => 0,
]
); // 100 100 руб.
Formatter::format(
'{{value|price(decimals, decimalSeparator, thousandsSeparator, allowZeroDecimal, round, roundPrecision, floor)}} руб.',
['value' => 100100.6],
[
'decimals' => 0,
'decimalSeparator' => '.',
'thousandsSeparator' => ' ',
'allowZeroDecimal' => false,
'round' => null,
'roundPrecision' => 0,
'floor' => true,
]
); // 100 100 руб.
use Fi1a\Format\Formatter;
Formatter::addShortcut('price', 'price(0, ".", " ", false, null, 0, true)');
Formatter::format('{{value|~price}} руб.', ['value' => 100100.6]); // 100 100 руб.
use Fi1a\Format\Formatter;
Formatter::format('{{if(foo)}}{{bar}}{{else}}false{{endif}}', ['foo' => true, 'bar' => 'bar']); // bar
Formatter::format('{{if(not_exists)}}{{foo}}{{elseif(bar)}}{{bar}}{{endif}}', ['foo' => 'foo', 'bar' => 'bar']); // bar
use Fi1a\Format\Formatter;
Formatter::format('{{if(value|unescape==="a&b")}}{{value}}{{endif}}', ['value' => 'a&b']); // "a&b"
use Fi1a\Format\Formatter;
Formatter::addSpecifier('spf', Specifier::class); // true
Formatter::format('{{foo|spf(true)}}', ['foo' => 'foo']); // foo
use Fi1a\Format\Formatter;
Formatter::addShortcut('dt', 'date("d.m.Y", "en")');
Formatter::format('{{foo|~dt}}', ['foo' => time()]); // 29.09.2022