The Stringler
A simple class to manipulate strings in an OO way. Inspired by Spatie's String. Just built this for fun. Hope you like it.
Install
Via composer:
composer require thestringler/manipulator
Using Laravel? Checkout The Stringler Laravel Package.
Methods
append($string)
Manipulator::make('Freak')->append(' Out!');
// Freak Out!
camelToSnake
Manipulator::make('camelCase')->camelToSnake();
// camel_case
camelToClass
Manipulator::make('className')->camelToClass();
// ClassName
capitalize
Manipulator::make('hello')->capitalize();
// Hello
capitalizeEach
Manipulator::make('i like toast!')->capitalizeEach();
// I Like Toast!
custom
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 string
eachCharacter($closure)
Manipulator::make('hello')->eachCharacter(function($char) {
return strtoupper($char);
});
// HELLO
eachWord($closure, $preserveSpaces = false)
Manipulator::make('hello moto')->eachWord(function($word) {
return strrev($word);
});
// ollehotom
Manipulator::make('hello moto')->eachWord(function($word) {
return strrev($word);
}, true);
// olleh otom
getPossessive
Manipulator::make('Bob')->getPossessive();
// Bob's
Manipulator::make('Silas')->getPossessive();
// Silas'
htmlEntities($flags = ENT_HTML5, $encoding = 'UTF-8', $doubleEncode = true)
Manipulator::make('&')->htmlEntities();
// &
htmlEntitiesDecode($flags = ENT_HTML5, $encoding = 'UTF-8')
Manipulator::make('&')->htmlEntitiesDecode();
// &
htmlSpecialCharacters($flags = ENT_HTML5, $encoding = 'UTF-8', $doubleEncode = true)
Manipulator::make('&<>')->htmlSpecialCharacters();
// &<>
lowercaseFirst
Manipulator::make('HELLO')->lowercaseFirst();
// hELLO
make($string)
// Named constructor
Manipulator::make('string');
pad($length, $string, $type = null)
Manipulator::make('Hello')->pad(2, '!!', STR_PAD_RIGHT);
// Hello!!
prepend($string)
Manipulator::make('is the one.')->prepend('Neo ');
// Neo is the one.
pluralize($items = null)
Manipulator::make('Potato')->pluralize();
// Potatoes
You 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);
// Cat
nthCharacter($nth, $closure)
Manipulator::make('Wordpress')->nthCharacter(5, function($character) {
return mb_strtoupper($character);
});
// WordPress
nthWord($nth, $closure, $preserveSpaces = true)
Manipulator::make('Oh hello there!')->nthWord(2, function($word) {
return mb_strtoupper($word);
});
// Oh HELLO there!
remove($string, $caseSensitive = true)
Manipulator::make('Dog Gone')->remove('Gone');
// Dog
removeSpecialCharacters($exceptions = [])
Manipulator::make('Hello!!')->removeSpecialCharacters();
// Hello
Manipulator::make('Hello!!')->removeSpecialCharacters(['!']);
// Hello!!
repeat($multiplier = 1)
Manipulator::make('la')->repeat(3);
// lalala
replace($find, $replace = '', $caseSensitive = true)
Manipulator::make('Pickles are good.')->replace('good', 'terrible');
// Pickles are terrible.
reverse
Manipulator::make('Whoa!')->reverse();
// !aohW
snakeToCamel
Manipulator::make('snake_case')->snakeToCamel();
// snakeCase
snakeToClass
Manipulator::make('class_name')->snakeToClass();
// ClassName
stripTags($allowed = '')
Manipulator::make('<i>Hello</i>')->stripTags();
// Hello
toCamelCase
Manipulator::make('camel case')->toCamelCase();
// camelCase
toL33t
Manipulator::make('Hack The Planet!')->toL33t();
// (-)@{|< +/-/€ |O7@|\|€][!
toLower
Manipulator::make('LOWER')->toLower();
// lower
toSlug
Manipulator::make('This is a slug!')->toSlug();
// this-is-a-slug
toSnakeCase
Manipulator::make('snake case')->toSnakeCase();
// snake_case
toString
This method just returns the string.
toUpper
Manipulator::make('upper')->toUpper();
// UPPER
trim
Manipulator::make(' trimmed ')->trim();
// trimmed
trimBeginning
Manipulator::make(' trimmed')->trimBeginning();
// trimmed
trimEnd
Manipulator::make('trimmed ')->trimEnd();
// trimmed
truncate($length = 100, $append = '...')
Manipulator::make('This is a sentence and will be truncated.')->truncate(10, '...');
// This is a ...
urlDecode
Manipulator::make('hello%21')->urlDecode();
// hello!
urlEncode
Manipulator::make('hello!')->urlEncode();
// hello%21
Chainable
All of these methods (minus toString
) can be chained.
Manipulator::make('hello')->toUpper()->reverse();
// OLLEH
Contribute
Contributions are very welcome!
- Follow the PSR-2 Standard
- Send a pull request.
That's pretty much it!