A simple class to manipulate strings in an OO way. Inspired by Spatie's String. Just built this for fun. Hope you like it.
Via composer:
composer require thestringler/manipulatorUsing Laravel? Checkout The Stringler Laravel Package.
Manipulator::make('Freak')->append(' Out!');
// Freak Out!Manipulator::make('camelCase')->camelToSnake();
// camel_caseManipulator::make('className')->camelToClass();
// ClassNameManipulator::make('hello')->capitalize();
// HelloManipulator::make('i like toast!')->capitalizeEach();
// I Like Toast!Manipulator::make('Some String')->custom(function ($string) {
// This is just a sample, this can be achieved with existing methods,
// But you can do whatever string manipulation you want here.
return ucfirst(strtolower($string));
});
// Some stringManipulator::make('hello')->eachCharacter(function($char) {
return strtoupper($char);
});
// HELLOManipulator::make('hello moto')->eachWord(function($word) {
return strrev($word);
});
// ollehotom
Manipulator::make('hello moto')->eachWord(function($word) {
return strrev($word);
}, true);
// olleh otomManipulator::make('Bob')->getPossessive();
// Bob's
Manipulator::make('Silas')->getPossessive();
// Silas'Manipulator::make('&')->htmlEntities();
// &Manipulator::make('&')->htmlEntitiesDecode();
// &Manipulator::make('&<>')->htmlSpecialCharacters();
// &<>Manipulator::make('HELLO')->lowercaseFirst();
// hELLO// Named constructor
Manipulator::make('string');Manipulator::make('Hello')->pad(2, '!!', STR_PAD_RIGHT);
// Hello!!Manipulator::make('is the one.')->prepend('Neo ');
// Neo is the one.Manipulator::make('Potato')->pluralize();
// PotatoesYou can optionally pass an array or numeric value to pluaralize to determine if the given string should be pluaralized.
$dogs = ['Zoe', 'Spot', 'Pickles'];
Manipulator::make('Dog')->pluralize($dogs);
// Dogs
$cats = ['Whiskers'];
Manipulator::make('Cat')->pluralize($cats);
// CatManipulator::make('Wordpress')->nthCharacter(5, function($character) {
return mb_strtoupper($character);
});
// WordPressManipulator::make('Oh hello there!')->nthWord(2, function($word) {
return mb_strtoupper($word);
});
// Oh HELLO there!Manipulator::make('Dog Gone')->remove('Gone');
// DogManipulator::make('Hello!!')->removeSpecialCharacters();
// Hello
Manipulator::make('Hello!!')->removeSpecialCharacters(['!']);
// Hello!!Manipulator::make('la')->repeat(3);
// lalalaManipulator::make('Pickles are good.')->replace('good', 'terrible');
// Pickles are terrible.Manipulator::make('Whoa!')->reverse();
// !aohWManipulator::make('snake_case')->snakeToCamel();
// snakeCaseManipulator::make('class_name')->snakeToClass();
// ClassNameManipulator::make('<i>Hello</i>')->stripTags();
// HelloManipulator::make('camel case')->toCamelCase();
// camelCaseManipulator::make('Hack The Planet!')->toL33t();
// (-)@{|< +/-/€ |O7@|\|€][!Manipulator::make('LOWER')->toLower();
// lowerManipulator::make('This is a slug!')->toSlug();
// this-is-a-slugManipulator::make('snake case')->toSnakeCase();
// snake_caseThis method just returns the string.
Manipulator::make('upper')->toUpper();
// UPPERManipulator::make(' trimmed ')->trim();
// trimmedManipulator::make(' trimmed')->trimBeginning();
// trimmedManipulator::make('trimmed ')->trimEnd();
// trimmedManipulator::make('This is a sentence and will be truncated.')->truncate(10, '...');
// This is a ...Manipulator::make('hello%21')->urlDecode();
// hello!Manipulator::make('hello!')->urlEncode();
// hello%21All of these methods (minus toString) can be chained.
Manipulator::make('hello')->toUpper()->reverse();
// OLLEHContributions are very welcome!
- Follow the PSR-2 Standard
- Send a pull request.
That's pretty much it!