A clean, elegant, unicode-safe, fluent, immutable, localisable, dependency-free string manipulation library for PHP 7.1+
You can install the package via composer:
composer require placemat/editor
To use Editor, first you've got to instantiate it on your string:
echo Editor::create('hello world')->upperCase();
// HELLO WORLD
That's a little verbose though, so Editor comes with a global helper function too:
echo s('hello world')->upperCase();
// HELLO WORLD
Editor is fluent and chainable, so you can just keep adding operations to each other:
echo s(' EDITOR is pretty neat I guess 💩 ')
->trim()
->titleCase();
// Editor is Pretty Neat I Guess 💩
Editor is also immutable, so you won't mess up your original instance:
$str = s('Apple');
echo $str->plural(); // Apples
echo $str; // Apple
Editor implements PHP's __toString()
magic method, so in most cases you can just use it like a string and it'll work just fine:
$str = s('worlds');
echo 'Hello ' . $str->singular()->upperCaseFirst();
// Hello World
If you do need to get a regular string back for whatever reason, you can either cast an instance of Editor, or call str()
:
$hello = s('Hello World');
(string) $hello; // Will be a string
$hello->str(); // Will also be a string
Editor implements proper title casing, based on John Gruber's crazy title casing script:
s('this is a headline that will be properly title-cased')->titleCase();
// This Is a Headline That Will Be Properly Title-Cased
If you're after what other libraries so boldly claim is title case, you want upperCaseFirst()
:
s('this is not actually title casing')->upperCaseFirst();
// This Is Not Actually Title-casing
Editor supports making a string singular or plural:
s('apples')->singular(); // 'apple'
s('apple')->plural(); // 'apples'
// plural also has a $count parameter, in case you need to pluralize dynamically
s('apple')->plural($count); // 'apple' if $count is 1, 'apples' otherwise
Inflections are localizable in Editor. Right now, Editor supports 6 languages:
- English ('en')
- Spanish ('es')
- French ('fr')
- Portuguese ('pt')
- Norwegian Bokmal ('nb')
- Turkish ('tr')
s('bijou')->plural($count, 'fr'); // 'bijoux'
If you'd like to add an inflector, there are a few requirements:
- It must extend
Placemat\Editor\Inflectors\Inflector
- It must be in the
Placemat\Editor\Inflectors
namespace- It must be named for the two-letter ISO language code, e.g. English ->
En
If you do add inflectors, feel free to open a Pull Request!
Full function reference documentation is probably something I should write. But for now, you can peruse the main Editor class. Every method is doc-blocked and tested 👍