xtompie / pipe
简单的管道实现
1.0.0
2024-09-07 14:10 UTC
Requires (Dev)
- phpunit/phpunit: ^11.3
This package is auto-updated.
Last update: 2024-09-08 15:02:35 UTC
README
Pipe 是一个用于流畅数据处理的功能类。
需求
- PHP >= 8
安装
使用 composer
composer require xtompie/pipe
文档
<?php use Xtompie\Pipe\Pipe; $result = Pipe::new(' <h1>Hello World</h1> ') ->trim() ->lower() ->ucfirst() ->htmlspecialchars() ->get(); echo $result;
输出将是: Hello world
可用方法
- addslashes() - 在需要转义的字符串字符前添加斜杠。
- explode(string $delimiter) - 通过指定的分隔符拆分字符串。
- htmlspecialchars() - 将特殊字符转换为 HTML 实体。
- implode(string $glue = ‘’) - 使用指定的粘合剂连接数组元素。
- lower() - 将所有字母字符转换为小写。
- map(callable $callback) - 将回调函数应用于当前值并返回一个包含转换后值的新的 Pipe 对象。
- md5() - 计算字符串的 MD5 散列。
- nl2br() - 在字符串中的所有换行符之前插入 HTML 换行。
- object(string $type) - 将当前值转换为指定的对象。
- replace(string $search, string $replace) - 将搜索字符串的替换字符串替换。
- reverse() - 反转字符串。
- sha1() - 计算字符串的 SHA-1 散列。
- length() - 返回字符串的长度。
- slice() - 提取数组的切片
- strpos(string $needle) - 查找子字符串第一次出现的位置。
- stripTags() - 从字符串中删除 HTML 和 PHP 标签。
- substr(int $start, int $length = null) - 返回字符串的一部分。
- trim() - 从字符串的开始和结束处删除空格。
- ucfirst() - 将字符串的第一个字母大写。
- ucwords() - 将字符串中每个单词的第一个字母大写。
- upper() - 将所有字母字符转换为大写。
创建特定的转换管道
您可以通过扩展 Pipe 类来创建具有自定义方法的特定管道。
<?php use Xtompie\Pipe\Pipe; class CustomPipe extends Pipe { public function reverseAndUpper(): static { return $this->reverse()->upper(); } } $result = CustomPipe::new('Hello World') ->reverseAndUpper() ->get(); echo $result;
输出将是: DLROW OLLEH