gears / string
一组基本字符串操作函数。
Requires
- php: ^5.4 || ^7.0
- icecave/parity: ^1.0
- voku/portable-utf8: ^3.0
Requires (Dev)
- codegyre/robo: ^0.7
- couscous/couscous: ^1.5
- icanboogie/inflector: ^1.4
- phpunit/phpunit: ^4.8
- satooshi/php-coveralls: ^1.0
- voku/anti-xss: ^2.0
- voku/urlify: ^2.0
This package is not auto-updated.
Last update: 2020-01-24 15:11:08 UTC
README
寻找维护者,我现在几乎不做PHP开发,我已经转向其他方向,现在主要做dotnet core、node.js和golang。如果有任何人对接管这些项目感兴趣,请联系我 - brad@bjc.id.au
字符串齿轮
一种面向对象的方式来处理PHP中的字符串,内置多字节支持。
致谢 & 启发
原始库 https://github.com/danielstjules/Stringy The voku分支 https://github.com/voku/Stringy
这个版本基于voku的工作。主要目标是通过将Stringy类中的大量方法拆分为特性来简化代码库的管理。
这意味着我们将最低PHP版本提升到5.4+
注意:还有一些其他的变化,这不是一个API兼容的分支。
如何安装
通过composer安装非常简单
composer require gears/string
然后导入您的脚本中的 Str
类
use Gears\String\Str;
面向对象和链式调用
该库提供了如下所示的对象方法链式调用
echo Str::s('fòô bàř')->removeWhitespace()->swapCase(); // 'FÒÔ BÀŘ'
Gears\String\Str
有一个 __toString()
方法,当对象在字符串上下文中使用时返回当前字符串,即: (string) Str::s('foo')
实现的接口
Gears\String\Str
实现了 IteratorAggregate
接口,这意味着可以使用 foreach
与类的实例一起使用
$str = Str::s('fòôbàř'); foreach ($str as $char) { echo $char; } // 'fòôbàř'
它实现了 Countable
接口,使得可以使用 count()
来获取字符串中的字符数
$str = Str::s('fòô'); count($str); // 3
此外,已实现 ArrayAccess
接口。因此,可以使用 isset()
来检查特定索引处的字符是否存在。由于 Gears\String\Str
是不可变的,对 offsetSet
或 offsetUnset
的任何调用都将抛出异常。已经实现了 offsetGet
,它接受正索引和负索引。无效索引将导致 OutOfBoundsException
。
$str = Str::s('bàř'); echo $str[2]; // 'ř' echo $str[-2]; // 'à' isset($str[-4]); // false $str[3]; // OutOfBoundsException $str[2] = 'a'; // Exception
方法
所有方法都使用PSR-5 docblocks进行了文档说明,为任何不错的IDE(如 PHP Storm 或 Atom与 atom-autocomplete-php)提供了自动完成提示。
由Brad Jones开发 - brad@bjc.id.au