chr15k/string 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chr15k/string

Composer 安装命令:

composer require chr15k/string

包简介

Awesome String helpers package

README 文档

README

Latest Stable Version Latest Unstable Version Total Downloads License

This package provides useful helpers for working with strings in PHP, including UUID and ASCII support.

Based on ...

Install

You can install this package via composer:

composer require chr15k/string

Usage

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"

Docs

String Method Chaining

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

Transliterate the string to an ASCII value:

$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

The whenEmpty method invokes the given Closure if the string is empty:

$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 >>>
String Methods

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()

The Str::orderedUuid() method generates a "timestamp first" UUID that may be efficiently stored in an indexed database column.

$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 >>>

Testing

You can run the tests with:

vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

chr15k/string 适用场景与选型建议

chr15k/string 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 66 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「helpers」 「string」 「chr15k」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 chr15k/string 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chr15k/string 我们能提供哪些服务?
定制开发 / 二次开发

基于 chr15k/string 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 66
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-04