chr15k/string

出色的字符串助手包

1.0.5 2020-08-04 16:16 UTC

This package is auto-updated.

Last update: 2024-09-05 01:14:17 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

此包为PHP中处理字符串提供了有用的助手,包括UUID和ASCII支持。

基于 ...

安装

您可以通过composer安装此包

composer require chr15k/string

用法

return s('Child')
    ->plural()
    ->possessive()
    ->append(' Book'); // outputs: "Children's Book"

return s('Child')
    ->plural(1); // (pass a count) outputs: "Child"

return s(' hello_world')
    ->trim()
    ->camel(); // outputs: "helloWorld"

return s('Hello World')
    ->studly(); // outputs: "HelloWorld"

return s('Hello World')
    ->slug('-'); // outputs: "hello-world"

文档

字符串方法链

Chain multiple string operations together using the s() helper.

after

$slice = s('This is my name')->after('This is');

// ' my name'

afterLast

$slice = s('App\Controllers\Controller')->afterLast('\\');

// 'Controller'

append

$string = s('Hello')->append(' there!');

// 'Hello there!'

ascii

将字符串转译为ASCII值

$string = s('ü')->ascii();

// 'u'

basename

$string = s('/foo/bar/baz')->basename();

// 'baz'

// If needed, you may provide an "extension" that will be removed from the trailing component:
$string = s('/foo/bar/baz.jpg')->basename('.jpg');

// 'baz'

before

$slice = s('This is my name')->before('my name');

// 'This is '

beforeLast

$slice = s('This is my name')->beforeLast('is');

// 'This '

camel

$converted = s('foo_bar')->camel();

// fooBar

contains

$contains = s('This is my name')->contains('my');

// true

// You can also pass an array:
$contains = s('This is my name')->contains(['my', 'foo']);

// true

containsAll

$containsAll = s('This is my name')->containsAll(['my', 'name']);

// true

dirname

$string = s('/foo/bar/baz')->dirname();

// '/foo/bar'

// Optionally pass directory levels as second argument:
$string = s('/foo/bar/baz')->dirname(2);

// '/foo'

endsWith

$result = s('This is my name')->endsWith('name');

// true

// You can also pass an array
$result = s('This is my name')->endsWith(['name', 'foo']);

// true

$result = s('This is my name')->endsWith(['this', 'foo']);

// false

exactly

$result = s('Chris')->exactly('Chris');

// true

$result = s(' Chris')->exactly('Chris');

// false

$result = s('Chris')->exactly('chris');

// false

explode

$collection = s('foo bar baz')->explode(' ');

// ['foo', 'bar', 'baz']

finish

$adjusted = s('this/string')->finish('/');

// this/string/

$adjusted = s('this/string/')->finish('/');

// this/string/

isAscii

$result = s('Chris')->isAscii();

// true

$result = s('ü')->isAscii();

// false

isEmpty

$result = s('  ')->trim()->isEmpty();

// true

$result = s('Chris')->trim()->isEmpty();

// false

isNotEmpty

$result = s('  ')->trim()->isNotEmpty();

// false

$result = s('Chris')->trim()->isNotEmpty();

// true

kebab

$converted = s('fooBar')->kebab();

// foo-bar

length

$length = s('Chris')->length();

// 5

limit

$truncated = s('The quick brown fox jumps over the lazy dog')->limit(20);

// The quick brown fox...

// pass second argument to append something other than '...'
$truncated = s('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)');

// The quick brown fox (...)

lower

$result = s('CHRIS')->lower();

// 'chris'

ltrim

$string = s('  Chris  ')->ltrim();

// 'Chris  '

$string = s('/Chris/')->ltrim('/');

// 'Chris/'

match

$result = s('foo bar')->match('/bar/');

// 'bar'

$result = s('foo bar')->match('/foo (.*)/');

// 'bar'

plural

$plural = s('car')->plural();

// cars

$plural = s('child')->plural();

// children

// Pass second argument as a count to determine singular or plural form of a string:
$plural = s('child')->plural(2);

// children

$plural = s('child')->plural(1);

// child

possessive

$possessive = s('Chris')->possessive();

// Chris'

$possessive = s('David')->possessive();

// David's

$possessive = s('it')->possessive();

// its

prepend

$string = s('World')->prepend('Hello ');

// Hello World

replace

$replaced = s('Hello World')->replace('World', 'Chris');

// Hello Chris

replaceArray

$string = 'The event will take place between ? and ?';

$replaced = s($string)->replaceArray('?', ['8:30', '9:00']);

// The event will take place between 8:30 and 9:00

replaceFirst

$replaced = s('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a');

// a quick brown fox jumps over the lazy dog

replaceLast

$replaced = s('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a');

// the quick brown fox jumps over a lazy dog

rtrim

$string = s('  Chris  ')->rtrim();

// '  Chris'

$string = s('/Chris/')->rtrim('/');

// '/Chris'

singular

$singular = s('cars')->singular();

// car

$singular = s('children')->singular();

// child

slug

$slug = s('Hello World')->slug('-');

// hello-world

snake

$converted = s('fooBar')->snake();

// foo_bar

split

$segments = s('one, two, three')->split('/[\s,]+/');

// collect(["one", "two", "three"])

start

$adjusted = s('this/string')->start('/');

// /this/string

$adjusted = s('/this/string')->start('/');

// /this/string

startsWith

$result = s('This is my name')->startsWith('This');

// true

studly

$converted = s('foo_bar')->studly();

// FooBar

substr

$string = s('Hello World')->substr(6);

// World

$string = s('Hello World')->substr(6, 3);

// Wo

title

$converted = s('a nice title uses the correct case')->title();

// A Nice Title Uses The Correct Case

trim

$string = s('  Chris  ')->trim();

// 'Chris'

$string = s('/Chris/')->trim('/');

// 'Chris'

ucfirst

$string = s('foo bar')->ucfirst();

// Foo bar

upper

$adjusted = s('chris')->upper();

// CHRIS

whenEmpty

whenEmpty 方法会在字符串为空时调用给定的闭包

$string = s('  ')->whenEmpty(function ($string) {
    return $string->trim()->prepend('Chris');
});

// 'Chris'

words

$string = s('Perfectly balanced, as all things should be.')->words(3, ' >>>');

// Perfectly balanced, as >>>
字符串方法

Str::after()

$slice = Str::after('This is my name', 'This is');

// ' my name'

Str::afterLast()

$slice = Str::afterLast('App\Controllers\Controller', '\\');

// 'Controller'

Str::before()

$slice = Str::before('This is my name', 'my name');

// 'This is '

Str::camel()

$converted = Str::camel('foo_bar')

// fooBar

Str::contains()

$contains = Str::contains('This is my name', 'my');

// true

Str::containsAll()

$containsAll = Str::containsAll('This is my name', ['my', 'name']);

// true

Str::endsWith()

$result = Str::endsWith('This is my name', 'name');

// true

Str::finish()

$adjusted = Str::finish('this/string', '/');

// this/string/

$adjusted = Str::finish('this/string/', '/');

// this/string/

Str::isAscii()

$isAscii = Str::isAscii('Chris');

// true

$isAscii = Str::isAscii('ü');

// false

Str::isUuid()

$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');

// true

$isUuid = Str::isUuid('chris');

// false

Str::kebab()

$converted = Str::kebab('fooBar');

// foo-bar

Str::length()

$length = Str::length('Chris');

// 5

Str::limit()

$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);

// The quick brown fox...

Str::lower()

$lower = Str::lower('CHRIS');

// chris

Str::match()

$matches = Str::match('foo*', 'foobar');

// true

$matches = Str::match('baz*', 'foobar');

// false

Str::orderedUuid()

Str::orderedUuid() 方法生成一个 "时间戳优先" 的 UUID,可以有效地存储在索引数据库列中。

$orderedUuid = Str::orderedUuid();

// 90f81d6c-b4f6-4b03-a82d-800058a21705

Str::plural()

$plural = Str::plural('bus');

// buses

$plural = Str::plural('child');

// children


// Pass second argument to retrieve the singular or plural form of the string...

$plural = Str::plural('child', 2);

// children

$plural = Str::plural('child', 1);

// child

Str::possessive()

$possessive = Str::possessive('Chris');

// Chris'

$possessive = Str::possessive('David');

// David's

$possessive = Str::possessive('it');

// its

Str::random()

$random = Str::random(40);

// odkX5tWGo3tb8hlNgdoVPjHxZR8xRzii1uFT1cxa

Str::replaceArray()

$string = 'The event will take place between ? and ?';

$replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);

// The event will take place between 8:30 and 9:00

Str::replaceFirst()

$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');

// a quick brown fox jumps over the lazy dog

Str::replaceLast()

$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');

// the quick brown fox jumps over a lazy dog

Str::singular()

$singular = Str::singular('cars');

// car

$singular = Str::singular('children');

// child

Str::slug()

$slug = Str::slug('Chris The Coder', '-');

// chris-the-coder

Str::snake()

$converted = Str::snake('fooBar');

// foo_bar

Str::start()

$adjusted = Str::start('this/string', '/');

// /this/string

$adjusted = Str::start('/this/string', '/');

// /this/string

Str::startsWith()

$result = Str::startsWith('This is my name', 'This');

// true

Str::studly()

$converted = Str::studly('foo_bar');

// FooBar

Str::title()

$converted = Str::title('a nice title uses the correct case');

// A Nice Title Uses The Correct Case

Str::ucfirst()

$string = Str::ucfirst('foo bar');

// Foo bar

Str::upper()

$string = Str::upper('chris');

// CHRIS

Str::uuid()

$uuid = Str::uuid();

// 0b1a9d6f-e2c7-489d-93f9-331108ebc314

Str::words()

return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');

// Perfectly balanced, as >>>

测试

您可以使用以下命令运行测试

vendor/bin/phpunit

许可证

麻省理工学院许可证(MIT)。请参阅许可证文件获取更多信息。