The Stringler Laravel Package
A Laravel package for The Stringler, a string manipulation class., (*1)
Install
Compsoser:, (*2)
composer require thestringler-laravel/manipulator
After composer has done its thing, add the package service provider to the array in /config/app.php
:, (*3)
TheStringlerLaravel\Manipulator\ManipulatorServiceProvider::class
Then add the facade to the aliases array, also in config/app.php
:, (*4)
'Manipulator' => TheStringlerLaravel\Manipulator\ManipulatorFacade::class,
Helper function:, (*5)
$string = manipulate('hello')->toUpper();
Methods
append($string)
manipulate('Freak')->append(' Out!');
// Freak Out!
camelToSnake
manipulate('camelCase')->camelToSnake();
// camel_case
camelToClass
manipulate('className')->camelToClass();
// ClassName
capitalize
manipulate('hello')->capitalize();
// Hello
capitalizeEach
manipulate('i like toast!')->capitalizeEach();
// I Like Toast!
eachCharacter($closure)
manipulate('hello')->eachCharacter(function($char) {
return strtoupper($char);
});
// HELLO
eachWord($closure, $preserveSpaces = false)
manipulate('hello moto')->eachWord(function($word) {
return strrev($word);
});
// ollehotom
manipulate('hello moto')->eachWord(function($word) {
return strrev($word);
}, true);
// olleh otom
getPossessive
manipulate('Bob')->getPossessive();
// Bob's
manipulate('Silas')->getPossessive();
// Silas'
htmlEntities($flags = ENT_HTML5, $encoding = 'UTF-8', $doubleEncode = true)
manipulate('&')->htmlEntities();
// &
htmlEntitiesDecode($flags = ENT_HTML5, $encoding = 'UTF-8')
manipulate('&')->htmlEntitiesDecode();
// &
htmlSpecialCharacters($flags = ENT_HTML5, $encoding = 'UTF-8', $doubleEncode = true)
manipulate('&<>')->htmlSpecialCharacters();
// &<>
lowercaseFirst
manipulate('HELLO')->lowercaseFirst();
// hELLO
pad($length, $string, $type = null)
manipulate('Hello')->pad(2, '!!', STR_PAD_RIGHT);
// Hello!!
prepend($string)
manipulate('is the one.')->prepend('Neo ');
// Neo is the one.
pluralize($items = null)
manipulate('Potato')->pluralize();
// Potatoes
You can optionally pass an array or numeric value to pluaralize
to determine if the given string should be pluaralized., (*6)
$dogs = ['Zoe', 'Spot', 'Pickles'];
manipulate('Dog')->pluralize($dogs);
// Dogs
$cats = ['Whiskers'];
manipulate('Cat')->pluralize($cats);
// Cat
nthCharacter($nth, $closure)
manipulate('Wordpress')->nthCharacter(5, function($character) {
return mb_strtoupper($character);
});
// WordPress
nthWord($nth, $closure, $preserveSpaces = true)
manipulate('Oh hello there!')->nthWord(2, function($word) {
return mb_strtoupper($word);
});
// Oh HELLO there!
remove($string, $caseSensitive = true)
manipulate('Dog Gone')->remove('Gone');
// Dog
removeSpecialCharacters($exceptions = [])
manipulate('Hello!!')->removeSpecialCharacters();
// Hello
manipulate('Hello!!')->removeSpecialCharacters(['!']);
// Hello!!
repeat($multiplier = 1)
manipulate('la')->repeat(3);
// lalala
replace($find, $replace = '', $caseSensitive = true)
manipulate('Pickles are good.')->replace('good', 'terrible');
// Pickles are terrible.
reverse
manipulate('Whoa!')->reverse();
// !aohW
snakeToCamel
manipulate('snake_case')->snakeToCamel();
// snakeCase
snakeToClass
manipulate('class_name')->snakeToClass();
// ClassName
manipulate('<i>Hello</i>')->stripTags();
// Hello
toCamelCase
manipulate('camel case')->toCamelCase();
// camelCase
toL33t
manipulate('Hack The Planet!')->toL33t();
// (-)@{|< +/-/€ |O7@|\|€][!
toLower
manipulate('LOWER')->toLower();
// lower
toSlug
manipulate('This is a slug!')->toSlug();
// this-is-a-slug
toSnakeCase
manipulate('snake case')->toSnakeCase();
// snake_case
toString
This method just returns the string., (*7)
toUpper
manipulate('upper')->toUpper();
// UPPER
trim
manipulate(' trimmed ')->trim();
// trimmed
trimBeginning
manipulate(' trimmed')->trimBeginning();
// trimmed
trimEnd
manipulate('trimmed ')->trimEnd();
// trimmed
truncate($length = 100, $append = '...')
manipulate('This is a sentence and will be truncated.')->truncate(10, '...');
// This is a ...
urlDecode
manipulate('hello%21')->urlDecode();
// hello!
urlEncode
manipulate('hello!')->urlEncode();
// hello%21
Chainable
All of these methods (minus toString
) can be chained., (*8)
manipulate('hello')->toUpper()->reverse();
// OLLEH