vantoozz / strings
面向对象字符串库
v1.0.0-rc1
2020-06-21 19:06 UTC
Requires
- php: ~7
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ~6|~8|~9
This package is auto-updated.
Last update: 2024-09-16 00:47:55 UTC
README
真正的面向对象字符串操作库
该库的目的是提供一种面向对象的字符串操作方法。它适用于任何实现了 Stringable
接口的对象。 https://wiki.php.net/rfc/stringable。
设置
只需运行
composer require vantoozz/strings
转换
<?php declare(strict_types=1); use Vantoozz\Strings\Transforms\Acronym; use Vantoozz\Strings\Transforms\CaseToggled; use Vantoozz\Strings\Transforms\Reversed; use Vantoozz\Strings\Transforms\SnakeCased; use function Vantoozz\Strings\str; require_once __DIR__ . '/vendor/autoload.php'; $string = str('PHP: Hypertext Preprocessor'); echo new Acronym($string) . PHP_EOL; echo new CaseToggled($string) . PHP_EOL; echo new Reversed($string) . PHP_EOL; echo new SnakeCased($string) . PHP_EOL;
将输出
PHP php: hYPERTEXT pREPROCESSOR rossecorperP txetrepyH :PHP p_h_p:_hypertext_preprocessor
可用的转换
- 缩写
- 驼峰式
- 大小写切换
- kebab-case
- 小写
- 帕斯卡大小写
- 反转
- 句子大小写
- snake-case
- 标题大小写
- 大写
连接
<?php declare(strict_types=1); use Vantoozz\Strings\Joins\EndingWith; use Vantoozz\Strings\Joins\Joined; use Vantoozz\Strings\Joins\StartingWith; use function Vantoozz\Strings\str; require_once __DIR__ . '/vendor/autoload.php'; $one = str('aabbc'); $two = str('ccddaa'); echo new Joined($one, $two) . PHP_EOL; echo new EndingWith($one, $two) . PHP_EOL; echo new StartingWith($one, $two) . PHP_EOL;
将输出
aabbcccddaa aabbccddaa ccddaabbc
可用的连接
- 连接
- 以...开始
- 以...结束
格式
<?php declare(strict_types=1); use Vantoozz\Strings\Exceptions\InvalidFormatException; use Vantoozz\Strings\Formats\Email; use function Vantoozz\Strings\str; require_once __DIR__ . '/vendor/autoload.php'; try { echo new Email(str('user@example.com')) . PHP_EOL; } catch (InvalidFormatException $e) { echo $e->getMessage() . PHP_EOL; } try { echo new Email(str('user%example.com')) . PHP_EOL; } catch (InvalidFormatException $e) { echo $e->getMessage() . PHP_EOL; }
将输出
user@example.com Invalid format
可用的格式
- 电子邮件
- 主机名
- IPv4
- IPv6
- Mac地址
- SHA1
- SHA256
- URL
组合
<?php declare(strict_types=1); use Vantoozz\Strings\Formats\Email; use Vantoozz\Strings\Joins\EndingWith; use Vantoozz\Strings\Transforms\CaseToggled; use function Vantoozz\Strings\str; require_once __DIR__ . '/vendor/autoload.php'; $username = str('User@'); $domain = str('@Example.Com'); echo new CaseToggled(new Email(new EndingWith($username, $domain))) . PHP_EOL;
将输出
uSER@eXAMPLE.cOM
测试
./vendor/bin/phpunit