decodelabs / dictum
Text formatting tools
Installs: 26 369
Dependents: 7
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.4
- decodelabs/cosmos: ^0.3
- decodelabs/exceptional: ^0.6.3
- decodelabs/fluidity: ^0.3.7
- decodelabs/kingdom: ^0.1
- decodelabs/nuance: ^0.1.9
- symfony/polyfill-mbstring: ^1.33
Requires (Dev)
- decodelabs/phpstan-decodelabs: ^0.7
- nesbot/carbon: ^3.10.2
This package is auto-updated.
Last update: 2025-08-21 15:59:37 UTC
README
Text formatting tools for PHP
Dictum provides a collection of commonly required text parsing and processing features.
Installation
Install via Composer:
composer require decodelabs/dictum
Usage
Formatters
The root Dictum class exposes a set of predictable text / key formatters which can be used to quickly prepare strings for specific actions.
use DecodeLabs\Dictum; echo Dictum::name('geoff-randomName'); // Geoff Random Name echo Dictum::firstName('geoff-randomName'); // Geoff echo Dictum::initials('geoff-randomName'); // GRN echo Dictum::initialsAndSurname('geoff-randomName'); // GR Name echo Dictum::initialMiddleNames('geoff-randomName'); // Geoff R Name echo Dictum::consonants('here\'s a Random-string of text'); // hr's Rndm-strng f txt echo Dictum::label('here\'s a Random-string of text'); // Here's a random string of text echo Dictum::id('here\'s a Random-string of text'); // HeresARandomStringOfText echo Dictum::camel('here\'s a Random-string of text'); // heresARandomStringOfText echo Dictum::constant('here\'s a Random-string of text'); // HERE_S_A_RANDOM_STRING_OF_TEXT echo Dictum::slug('here\'s a Random-string of text / other stuff'); // heres-a-random-string-of-text-other-stuff echo Dictum::pathSlug('here\'s a Random-string of text / other stuff'); // heres-a-random-string-of-text/other-stuff echo Dictum::actionSlug('here\'s a Random-string of text / other stuff'); // here's-a-random-string-of-text-/-other-stuff echo Dictum::fileName('here\'s a Random-string of text / other stuff'); // here's-a-Random-string-of-text-_-other-stuff echo Dictum::shorten('here\'s a Random-string of text / other stuff', 10); // here's a… echo Dictum::numericToAlpha(23345452); // aybfra echo Dictum::alphaToNumeric('aybfra') // 23345452 echo Dictum::toBoolean('yes') ? 'true' : 'false'; // true
Text
The formatters above predominantly use the Text
class to process the strings provided. This class exposes a full suite of multibyte aware string manipulation functionality in an immutable collection format.
For example, the above id()
method is defined as:
echo (new Text($id)) ->toUtf8() ->toAscii() ->regexReplace('([^ ])([A-Z])', '\\1 \\2') ->replace(['-', '.', '+'], ' ') ->regexReplace('[^a-zA-Z0-9_ ]', '') ->toTitleCase() ->replace(' ', '') ->__toString();
Note, regexes are based off the mb_ereg functions and as such do not use delimiters in their patterns.
Cosmos extensions
Cosmos provides generic interfaces for defining locale-aware formatter extensions that can be implemented by different output generators.
Currently, Time and Number are available, defining predictable methods for formatting dates and various forms of number.
Dictum offers a plain text version of these interfaces:
use DecodeLabs\Dictum\Number; use DecodeLabs\Dictum\Time; // Custom format Time::format('now', 'd/m/Y', 'Europe/London'); // Locale format // When timezone is true it is fetched from Cosmos Time::locale('now', 'long', 'long', true); // Locale shortcuts Time::dateTime('tomorrow'); // medium Time::longTime('yesterday'); Time::shortDate('yesterday'); // ...etc // Intervals Time::since('yesterday'); // 1 day ago Time::until('tomorrow'); // 1 day from now Time::sinceAbs('yesterday'); // 1 day Time::untilAbs('yesterday'); // -1 day Time::between('yesterday', 'tomorrow'); // 1 day // Numbers Number::format(16.5, 'px'); // 16.5 px Number::format(16.5, 'px', 'de'); // 16,5 px Number::decimal(16.534643, 2); // 16.53 Number::currency(16.534643, 'GBP'); // £16.53 Number::percent(16.534643, 50, 2); // 33.07% Number::scientific(16.534643); // 1.6534643E1 Number::spellout(16.534643); // sixteen point five three four six four three Number::ordinal(16.534643); // 17th Number::diff(16.534643); // ⬆ 16.535 Number::fileSize(16534643); // 15.77 MiB Number::fileSizeDec(16534643); // 16.53 MB
See Tagged for equivalent HTML implementations of these interfaces.
Licensing
Dictum is licensed under the MIT License. See LICENSE for the full license text.