wuzzitor / string-util
提供了一层一致且自我解释的字符串操作功能。
1.0.0
2015-07-19 23:21 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ~4.0
- satooshi/php-coveralls: ~0.6
This package is not auto-updated.
Last update: 2024-09-14 17:58:53 UTC
README
概述
此库提供了一层一致且自我解释的字符串相关任务。
方法名称的说话方式使字符串操作更易读,并清晰地揭示了意图。请比较以下关于可读性的常见代码片段
// Native:
if (strpos($log, 'failure') !== false) {
// [...]
}
// With StringUtil:
if (StringUtil::contains($log, 'failure')) {
// [...]
}
与内置的PHP字符串函数相比,此库提供了具有一致签名的函数,这些函数始终将主题字符串作为第一个参数。
已知限制
此库旨在进行常见的轻量级字符串操作。目前尚未设计用于透明字符集处理等重型、复杂字符串任务。
安装
将以下内容添加到您的composer.json文件中(请参阅getcomposer.org)
"require" : {
// ...
"wuzzitor/string-util": "~1.0"
}
概念
此库提供了一个单类(Wuzzitor\StringUtil
),其中静态方法是所有字符串操作的入口点。
所有方法都是无状态的,以确保完全可测试性和避免难以调试的代码。
使用方法
示例
use Wuzzitor\StringUtil;
$filename = 'example.php';
$withoutExtension = StringUtil::removeSuffix($filename, '.php);
// $withoutExtension == 'example'
以下签名显示了所有可用的操作
StringUtil::startsWith(string $subject, string $prefix) : boolean
StringUtil::endsWith(string $subject, string $suffix) : boolean
StringUtil::contains(string $subject, string $needle) : boolean
StringUtil::containsAny(string $subject, string[] $needles) : boolean
StringUtil::containsAll(string $subject, string[] $needles) : boolean
StringUtil::removePrefix(string $subject, string $prefix) : string
StringUtil::removeSuffix(string $subject, string $suffix) : string
StringUtil::replace(string $subject, string $search, string $replace) : string
StringUtil::replace(string $subject, array<string, string> $searchReplaceMapping) : string
许可证
代码根据MIT许可证发布。