thestringler-laravel / manipulator
The Stringler 的 Laravel 扩展包
v1.1.0
2016-07-17 22:51 UTC
Requires
- thestringler/manipulator: ^0.5.0
This package is auto-updated.
Last update: 2024-09-24 04:30:21 UTC
README
A Laravel 扩展包,用于 The Stringler,一个字符串操作类。
安装
Compsoser
composer require thestringler-laravel/manipulator
在 Compsoser 完成其任务后,将包服务提供者添加到 /config/app.php
中的数组中
TheStringlerLaravel\Manipulator\ManipulatorServiceProvider::class
然后,将外观添加到别名数组中,也在 config/app.php
'Manipulator' => TheStringlerLaravel\Manipulator\ManipulatorFacade::class,
辅助函数
$string = manipulate('hello')->toUpper();
方法
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
你可以选择性地传递一个数组或数值给 pluaralize
来确定给定的字符串是否应该被复数化。
$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
stripTags($allowed = '')
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
此方法仅返回字符串。
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
链式调用
所有这些方法(除 toString
外)都可以链式调用。
manipulate('hello')->toUpper()->reverse(); // OLLEH