litgroup / str
该包已被放弃,不再维护。未建议替代包。
PHP 字符串工具。
0.2.1
2017-09-06 20:07 UTC
Requires
- php: ^7.1
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^6.3
This package is auto-updated.
Last update: 2023-11-12 22:36:11 UTC
README
PHP 字符串工具。
安装
composer require litgroup/str:^0.2.1
使用示例
UTF-8 字符串操作
存在一个 Str
类,其中包含一系列字符串操作方法。这些方法内部使用 mb_string
,但您不必担心编码问题。Str
方法总是使用 UTF-8 编码。
<?php use LitGroup\Str\Str; Str::length('hello'); // => 5 Str::isEmpty('hello'); // => false Str::isNotEmpty('hello'); // => true Str::trim(' hello '); // => 'hello' // ...
查看代码以查看所有方法。
使用模式
有时您需要检查字符串是否与模式匹配。为此,该库提供了 Pattern
接口。RegExp
是 Pattern
的一个实现,它代表 Perl 兼容正则表达式 (PCRE)。
<?php use LitGroup\Str\RegExp; $emailPattern = new RegExp('/^\w+(?:[-+.\']\w+)*@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*$/Dsu'); if ($emailPattern->isSatisfiedBy('john@example.com')) { echo 'This is a valid email address!'; }