thestringler/manipulator

面向对象的字符串操作方法。

v2.2.1 2019-08-28 01:22 UTC

README

一个简单的类,用于以面向对象的方式操作字符串。受 Spatie's String 启发。只是为了好玩而构建的。希望你会喜欢。

安装

通过 composer

composer require thestringler/manipulator

使用 Laravel?查看 The Stringler Laravel 包

方法

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();
// &amp;&lt;&gt;

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

你可以可选地传递一个数组或数字值到 pluaralize 来确定给定的字符串是否应该被复数化。

$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

此方法仅返回字符串。

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

链式调用

除了 toString 之外,所有这些方法都可以链式调用。

Manipulator::make('hello')->toUpper()->reverse();
// OLLEH

贡献

欢迎贡献!

  1. 遵循 PSR-2 标准规范
  2. 发送 pull request。

就这些了!