cdyun / php-tool
PHP8.1+通用工具包,包含数组处理、字符串处理、时间处理、加解密、IP、地理计算、Curl、全局辅助方法等功能
Installs: 23
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/cdyun/php-tool
Requires
- php: >=8.1
- ext-gd: *
- ext-json: *
- ext-mbstring: *
- ext-openssl: *
- ext-sodium: *
- guzzlehttp/guzzle: ^7.9
README
- PHP8.1+ 通用工具包
- 简介
- 安装
- 模块化目录
- 数组处理模块
- 字符串处理模块
- 特性
- 核心方法
- 使用示例
- 脱敏
- 字符串长度和截断
- 字符串命名转换
- 字符串查找和判断
- 字符串替换
- 字符串分割和连接
- 字符串去除空白
- 字符串填充
- 字符串重复
- 字符串随机
- 字符串编码解码
- 字符串哈希
- 字符串验证
- isEmail() - 是否为邮箱
- isUrl() - 是否为URL
- isIp() - 是否为IP地址
- isIpv4() - 是否为IPv4地址
- isIpv6() - 是否为IPv6地址
- isPhone() - 是否为手机号
- isIdCard() - 是否为身份证号
- isBankCard() - 是否为银行卡号
- isNumeric() - 是否为数字
- isAlpha() - 是否为字母
- isAlnum() - 是否为字母数字
- isHex() - 是否为十六进制
- isBinary() - 是否为二进制
- isJson() - 是否为JSON
- isXml() - 是否为XML
- isSerialized() - 是否为序列化数据
- isBase64() - 是否为Base64
- 字符串转换
- 字符串格式化
- 字符串字符操作
- 字符串统计
- 字符串安全
- 字符串比较
- 时间处理模块
- 数学计算处理模块
- 地理位置处理模块
- 特性
- 核心方法
- 使用示例
- distance() - 计算两个坐标之间的距离
- isValid() - 坐标验证
- midpoint() - 计算两个坐标之间的中点
- bearing() - 计算两个坐标之间的方位角
- toRadians() - 将角度转换为弧度
- toDegrees() - 将弧度转换为角度
- toDMS() - 将十进制度数转换为度分秒(DMS)
- toDecimal() - 将度分秒(DMS)转换为十进制度数
- gcj02ToBd09() - GCJ02坐标转BD09坐标
- bd09ToGcj02() - BD09坐标转GCJ02坐标
- wgs84ToGcj02() - WGS84坐标转GCJ02坐标(中国火星坐标系)
- gcj02ToWgs84() - GCJ02坐标转WGS84坐标
- wgs84ToBd09() - WGS84坐标转BD09坐标
- bd09ToWgs84() - BD09坐标转WGS84坐标
- IP地址处理模块
- 代码生成模块
- 加解密处理模块
- HTTP请求处理模块
- 版本要求
- 许可证
简介
这是一个基于PHP8.1+特性开发的模块化通用工具包,提供了数组处理、字符串处理、时间处理、加解密、消息体、IP地址处理、地理计算、全局辅助方法等功能。支持对象和静态两种调用方式,兼容进程和协程环境。
安装
composer require cdyun/php-tool
模块化目录
工具包采用清晰的模块化架构,每个模块专注于特定领域的功能:
├── src/
│ ├── Arr.php // 数组处理主类
│ ├── Str.php // 字符串处理主类
│ ├── Time.php // 时间处理主类
│ ├── Math.php // 数学计算主类
│ ├── Geo.php // 地理位置主类
│ ├── Ip.php // IP地址处理主类
│ ├── Crypto.php // 加解密主类(命名简洁)
│ ├── Generate.php // 代码生成工具类
│ ├── Curl.php // HTTP请求主类
│ ├── helpers.php // 全局辅助函数文件
│ ├──...... // 其他│
│
├── composer.json // Composer配置
├── README.md // 使用文档
数组处理模块
特性
- ✅ 树形结构转换(数组转树、树转数组)
- ✅ 层级结构转换
- ✅ 路径结构转换
- ✅ 常用数组操作(get/set/has/only/except)
- ✅ 数组深度合并
- ✅ 多维数组分组
- ✅ 多维数组统计(count/sum/avg/max/min)
- ✅ 多维数组转JSON/JSON转数组
- ✅ 非递归算法,支持大数据量
- ✅ 支持点语法和数组嵌套键访问
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
first() |
获取第一个元素 | Arr::first([1, 2, 3]) |
last() |
获取最后一个元素 | Arr::last([1, 2, 3]) |
find() |
查找满足条件的元素 | Arr::find([1, 2, 3], fn($n) => $n > 1) |
tree() |
数组转树形结构 | Arr::tree($list, 'id', 'pid') |
list() |
树形结构转数组 | Arr::list($tree) |
deepMerge() |
深度合并数组 | Arr::deepMerge($arr1, $arr2) |
兼容性说明
数组处理模块会自动检测PHP版本,在PHP 8.4+环境下使用原生数组函数以获得最佳性能:
array_first()- 获取数组第一个元素array_last()- 获取数组最后一个元素array_find()- 查找满足条件的元素array_find_key()- 查找满足条件的键名array_any()- 检查是否存在满足条件的元素array_all()- 检查是否所有元素都满足条件
在PHP 8.4以下版本,模块会使用兼容的实现,确保代码在不同PHP版本下都能正常运行。
使用示例
树形结构转换
tree() / toTree() - 数组转树形结构
use Cdyun\PhpTool\Arr; $data = [ ['id' => 1, 'name' => '部门1', 'parent_id' => 0], ['id' => 2, 'name' => '部门2', 'parent_id' => 1], ['id' => 3, 'name' => '部门3', 'parent_id' => 1], ['id' => 4, 'name' => '部门4', 'parent_id' => 2], ['id' => 5, 'name' => '部门5', 'parent_id' => 100] ]; // 按指定根节点ID,将扁平化数组转换为树形结构,会过滤掉非指定根节点及子节点的数据 $tree = Arr::toTree($data, 5, 'id', 'parent_id', 'children'); // 输出: // [ // [ // 'id' => 5, // 'name' => '部门5', // 'parent_id' => 100, // 'children' => [] // ] // ] // 数组转树形结构 $tree = Arr::tree($data, 'id', 'parent_id', 'children'); // 输出: // [ // [ // 'id' => 1, // 'name' => '部门1', // 'parent_id' => 0, // 'children' => [ // [ // 'id' => 2, // 'name' => '部门2', // 'parent_id' => 1, // 'children' => [ // ['id' => 4, 'name' => '部门4', 'parent_id' => 2, 'children' => []] // ] // ], // [ // 'id' => 3, // 'name' => '部门3', // 'parent_id' => 1, // 'children' => [] // ] // ] // ], // [ // 'id' => 5, // 'name' => '部门5', // 'parent_id' => 100, // 'children' => [] // ] // ]
list() - 树形结构转数组
$flat = Arr::list($tree, 'children');
level() - 数组转层级结构
$level = Arr::level($data, 'id', 'parent_id', 'level'); // 输出: // [ // ['id' => 1, 'name' => '部门1', 'parent_id' => 0, 'level' => 1], // ['id' => 2, 'name' => '部门2', 'parent_id' => 1, 'level' => 2], // ['id' => 3, 'name' => '部门3', 'parent_id' => 1, 'level' => 2], // ['id' => 4, 'name' => '部门4', 'parent_id' => 2, 'level' => 3] // ]
path() - 数组转路径结构
$path = Arr::path($data, 'id', 'parent_id', 'name', 'path', '/'); // 输出: // [ // ['id' => 1, 'name' => '部门1', 'parent_id' => 0, 'path' => '部门1'], // ['id' => 2, 'name' => '部门2', 'parent_id' => 1, 'path' => '部门1/部门2'], // ['id' => 3, 'name' => '部门3', 'parent_id' => 1, 'path' => '部门1/部门3'], // ['id' => 4, 'name' => '部门4', 'parent_id' => 2, 'path' => '部门1/部门2/部门4'] // ]
数组访问和操作
deepMerge() - 数组深度合并
use Cdyun\PhpTool\Arr; $arr1 = [ 'user' => ['name' => '张三', 'age' => 25], 'settings' => ['theme' => 'dark'] ]; $arr2 = [ 'user' => ['age' => 26, 'email' => 'user@example.com'], 'settings' => ['language' => 'zh-CN'] ]; $merged = Arr::deepMerge($arr1, $arr2); // 输出: // [ // 'user' => ['name' => '张三', 'age' => 26,'email' => 'user@example.com'], // 'settings' => ['theme' => 'dark', 'language' => 'zh-CN'] // ]
get() - 数组获取值(支持点语法)
$value = Arr::get($arr1, 'user.name'); // '张三' $value = Arr::get($arr1, ['user', 'age']); // 25 $value = Arr::get($arr1, 'user.email', 'default@example.com'); // 'default@example.com'
set() - 数组设置值
$result = Arr::set($arr1, 'user.age', 26);
has() - 数组判断是否存在键
$exists = Arr::has($arr1, 'user.name'); // true $exists = Arr::has($arr1, 'user.email'); // false
only() - 数组仅保留指定键
$only = Arr::only($arr1, ['user.name', 'user.age']); // 输出: ['user' => ['name' => '张三', 'age' => 25]]
except() - 数组排除指定键
$except = Arr::except($array1, ['settings']); // 输出: ['user' => ['name' => '张三', 'age' => 25]]
多维数组处理
group() - 多维数组分组
use Cdyun\PhpTool\Arr; $flatArray = [ ['id' => 1, 'name' => '部门1', 'parent_id' => 0], ['id' => 2, 'name' => '部门2', 'parent_id' => 1], ['id' => 3, 'name' => '部门3', 'parent_id' => 1], ['id' => 4, 'name' => '部门4', 'parent_id' => 2] ]; // 多维数组分组 $grouped = Arr::group($flatArray, 'parent_id'); // 输出: // [ // 0 => [['id' => 1, 'name' => '部门1', 'parent_id' => 0]], // 1 => [ // ['id' => 2, 'name' => '部门2', 'parent_id' => 1], // ['id' => 3, 'name' => '部门3', 'parent_id' => 1] // ], // 2 => [['id' => 4, 'name' => '部门4', 'parent_id' => 2]] // ]
count() - 多维数组统计
// 多维数组统计 $count = Arr::count($flatArray, 'parent_id'); // 输出: [0 => 1, 1 => 2, 2 => 1]
sum() - 多维数组求和
$sum = Arr::sum($flatArray, 'id'); // 10
avg() - 多维数组求平均值
$avg = Arr::avg($flatArray, 'id'); // 2.5
max() - 多维数组求最大值
$min = Arr::max($flatArray, 'id'); // 4
min() - 多维数组求最小值
$min = Arr::min($flatArray, 'id'); // 1
PHP 8.4+数组函数
first() - 数组首元素
use Cdyun\PhpTool\Arr; // PHP 8.4+使用原生array_first $first = Arr::first([1, 2, 3, 4, 5]); // 1 $first = Arr::first([]); // null
last() - 数组尾元素
// PHP 8.4+使用原生array_last $last = Arr::last([1, 2, 3, 4, 5]); // 5 $last = Arr::last([]); // null
find() - 查找满足条件的元素
// PHP 8.4+使用原生array_find $found = Arr::find([1, 2, 3, 4, 5], fn($n) => $n > 2); // 3 $found = Arr::find([1, 2, 3, 4, 5], fn($n) => $n > 10); // null
findKey() - 查找满足条件的键名
// PHP 8.4+使用原生array_find_key $foundKey = Arr::findKey(['a' => 1, 'b' => 2, 'c' => 3], fn($n) => $n > 1); // 'b' $foundKey = Arr::findKey(['a' => 1, 'b' => 2, 'c' => 3], fn($n) => $n > 10); // null
any() - 检查是否存在满足条件的元素
// PHP 8.4+使用原生array_any $hasAny = Arr::any([1, 2, 3, 4, 5], fn($n) => $n > 3); // true $hasAny = Arr::any([1, 2, 3, 4, 5], fn($n) => $n > 10); // false
all() - 检查是否所有元素都满足条件
// PHP 8.4+使用原生array_all $allMatch = Arr::all([1, 2, 3, 4, 5], fn($n) => $n > 0); // true $allMatch = Arr::all([1, 2, 3, 4, 5], fn($n) => $n > 2); // false
数组查找和判断
map() - 数组映射
$mapped = Arr::map([1, 2, 3, 4, 5], fn($n) => $n * 2); // [2, 4, 6, 8, 10]
filter() - 数组过滤
$filtered = Arr::filter([1, 2, 3, 4, 5], fn($n) => $n > 2); // [3, 4, 5] $filtered = Arr::filter([1, 2, 0, null, false]); // [1, 2](过滤空值)
reduce() - 数组归约
$sum = Arr::reduce([1, 2, 3, 4, 5], fn($carry, $n) => $carry + $n, 0); // 15
find() - 查找满足条件的第一个元素的键值
$findVal = Arr::find([1, 2, 3, 4, 5], fn($n) => $n > 3); // 4 $findVal = Arr::find(['a' => 10, 'b' => 20, 'c' => 30], fn($n) => $n > 20); // 30
findKey() - 查找满足条件的第一个元素的键名
$findKeyVal = Arr::findKey([1, 2, 3, 4, 5], fn($n) => $n > 3); // 3 $findKeyVal = Arr::findKey(['a' => 10, 'b' => 20, 'c' => 30], fn($n) => $n > 20); // 'c'
some() - 检查是否存在满足条件的元素(别名方法)
$hasSome = Arr::some([1, 2, 3, 4, 5], fn($n) => $n > 3); // true
every() - 检查是否所有元素都满足条件(别名方法)
$allMatch = Arr::every([1, 2, 3, 4, 5], fn($n) => $n > 0); // true
contains() - 数组是否包含指定元素
$contains = Arr::contains([1, 2, 3, 4, 5], 3); // true $contains = Arr::contains([1, 2, 3, 4, 5], '3', false); // true(非严格比较) $contains = Arr::contains([1, 2, 3, 4, 5], '3', true); // false(严格比较)
containsKey() - 数组是否包含指定键名
$containsKey = Arr::containsKey(['a' => 1, 'b' => 2], 'a'); // true
isEmpty() - 数组是否为空
$isEmpty = Arr::isEmpty([]); // true $isEmpty = Arr::isEmpty([1, 2, 3]); // false
isAssoc() - 数组是否为关联数组
$isAssoc = Arr::isAssoc(['a' => 1, 'b' => 2]); // true $isAssoc = Arr::isAssoc([1, 2, 3]); // false
isIndexed() - 数组是否为索引数组
$isIndexed = Arr::isIndexed([1, 2, 3]); // true $isIndexed = Arr::isIndexed(['a' => 1, 'b' => 2]); // false
数组排序
use Cdyun\PhpTool\Arr; $users = [ ['id' => 3, 'name' => '张三', 'age' => 25], ['id' => 1, 'name' => '李四', 'age' => 30], ['id' => 2, 'name' => '王五', 'age' => 28] ];
sort() - 升序/降序
// 升序 $sorted = Arr::sort($users, 'age', 'asc'); // 输出: // [ // ['id' => 3, 'name' => '张三', 'age' => 25], // ['id' => 2, 'name' => '王五', 'age' => 28], // ['id' => 1, 'name' => '李四', 'age' => 30] // ] // 降序 $sorted = Arr::sort($users, 'age', 'desc'); // 输出: // [ // ['id' => 1, 'name' => '李四', 'age' => 30], // ['id' => 2, 'name' => '王五', 'age' => 28], // ['id' => 3, 'name' => '张三', 'age' => 25] // ]
multiSort() - 多维数组排序
// 先按age升序,age相同时按id降序 $multiSorted = Arr::multiSort($users, ['age', 'id'], ['asc', 'desc']);
reverse() - 数组反转
$reversed = Arr::reverse([1, 2, 3, 4, 5]); // [5, 4, 3, 2, 1] $reversed = Arr::reverse(['a' => 1, 'b' => 2], true); // ['b' => 2, 'a' => 1](保留键名)
shuffle() - 数组打乱
$shuffled = Arr::shuffle([1, 2, 3, 4, 5]); // 随机打乱顺序
数组去重
unique() - 数组去重
$unique = Arr::unique([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]); // [1, 2, 3, 4] $unique = Arr::unique([1, '1', 2, '2'], true); // [1, '1', 2, '2](严格比较) $unique = Arr::unique([1, '1', 2, '2'], false); // [1, 2](非严格比较)
multiUnique() - 多维数组去重
$users = [ ['id' => 1, 'name' => '张三'], ['id' => 2, 'name' => '李四'], ['id' => 1, 'name' => '张三'] ]; $unique = Arr::multiUnique($users, 'id'); // 输出: // [ // ['id' => 1, 'name' => '张三'], // ['id' => 2, 'name' => '李四'] // ]
数组分页和切片
paginate() - 数组分页
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; $page1 = Arr::paginate($array, 1, 5); // [1, 2, 3, 4, 5] $page2 = Arr::paginate($array, 2, 5); // [6, 7, 8, 9, 10] $page3 = Arr::paginate($array, 3, 5); // [11, 12]
slice() - 数组切片
$sliced = Arr::slice([1, 2, 3, 4, 5], 1, 3); // [2, 3, 4] $sliced = Arr::slice(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], 1, 2, true); // ['b' => 2, 'c' => 3](保留键名)
chunk() - 数组分割
$chunked = Arr::chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3); // 输出: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
数组合并和差集
merge() - 数组合并
$merged = Arr::merge([1, 2, 3], [4, 5, 6]); // [1, 2, 3, 4, 5, 6]
mergeRecursive() - 数组合并(保留键名)
$merged = Arr::mergeRecursive(['a' => 1], ['b' => 2]); // ['a' => 1, 'b' => 2]
diff() - 数组差集
$diff = Arr::diff([1, 2, 3, 4, 5], [2, 4]); // [1, 3, 5]
diffKey() - 数组差集(带键名)
$diff = Arr::diffKey(['a' => 1, 'b' => 2, 'c' => 3], ['b' => 2]); // ['a' => 1, 'c' => 3]
intersect() - 数组交集
$intersect = Arr::intersect([1, 2, 3, 4, 5], [2, 4, 6]); // [2, 4]
intersectKey() - 数组交集(带键名)
$intersect = Arr::intersectKey(['a' => 1, 'b' => 2, 'c' => 3], ['b' => 2, 'c' => 4]); // ['b' => 2, 'c' => 3]
数组转换
toJson() - 多维数组转JSON
$json = Arr::toJson(['name' => '张三', 'age' => 25]); // '{"name":"张三","age":25}'
fromJson() - JSON转多维数组
$array = Arr::fromJson('{"name":"张三","age":25}'); // ['name' => '张三', 'age' => 25]
flatten() - 数组扁平化
$flattened = Arr::flatten([1, [2, [3, [4, 5]]]]); // [1, 2, 3, 4, 5]
keys() - 数组键名
$keys = Arr::keys(['a' => 1, 'b' => 2, 'c' => 3]); // ['a', 'b', 'c']
values() - 数组键值
$values = Arr::values(['a' => 1, 'b' => 2, 'c' => 3]); // [1, 2, 3]
flip() - 键名与键值翻转
$flipped = Arr::flip(['a' => 1, 'b' => 2, 'c' => 3]); // [1 => 'a', 2 => 'b', 3 => 'c']
column() - 数组列提取
$users = [ ['id' => 1, 'name' => '张三', 'age' => 25], ['id' => 2, 'name' => '李四', 'age' => 30] ]; $names = Arr::column($users, 'name'); // ['张三', '李四'] $indexed = Arr::column($users, 'name', 'id'); // [1 => '张三', 2 => '李四']
数组键值操作
mapKeys() - 数组映射键名
$mapped = Arr::mapKeys(['a' => 1, 'b' => 2], fn($key) => strtoupper($key)); // ['A' => 1, 'B' => 2]
mapValues() - 数组映射键值
$mapped = Arr::mapValues(['a' => 1, 'b' => 2], fn($value) => $value * 2); // ['a' => 2, 'b' => 4]
combine() - 数组合并键值
$combined = Arr::combine(['a', 'b', 'c'], [1, 2, 3]); // ['a' => 1, 'b' => 2, 'c' => 3]
fillKeys() - 数组填充键值
$filled = Arr::fillKeys(['a', 'b', 'c'], 0); // ['a' => 0, 'b' => 0, 'c' => 0]
fill() - 数组填充
$filled = Arr::fill(0, 5, 0); // [0, 0, 0, 0, 0]
数组随机操作
random() - 数组随机元素
$random = Arr::random([1, 2, 3, 4, 5]); // 随机返回其中一个元素
randomMany() - 数组随机多个元素
$randomMany = Arr::randomMany([1, 2, 3, 4, 5], 3); // 随机返回3个元素
数组元素操作
$array = [1, 2, 3, 4, 5];
shift() - 数组弹出第一个元素
$first = Arr::shift($array); // 1,$array变为[2, 3, 4, 5]
pop() - 数组弹出最后一个元素
$last = Arr::pop($array); // 5,$array变为[2, 3, 4]
unshift() - 数组头部添加元素
$count = Arr::unshift($array, 0); // 4,$array变为[0, 2, 3, 4]
push() - 数组尾部添加元素
$count = Arr::push($array, 5); // 5,$array变为[0, 2, 3, 4, 5]
remove() - 数组删除指定元素
$removed = Arr::remove([1, 2, 3, 2, 4, 2], 2); // [1, 3, 4]
removeKey() - 数组删除指定键名
$removed = Arr::removeKey(['a' => 1, 'b' => 2, 'c' => 3], 'b'); // ['a' => 1, 'c' => 3]
字符串处理模块
特性
- ✅ 随机字符串生成
- ✅ 命名转换(驼峰、蛇形、大驼峰)
- ✅ 字符串转义和反转义
- ✅ 字符串修剪
- ✅ 字符串脱敏(手机号、身份证号、邮箱、银行卡号、车牌号、姓名)
- ✅ 字符串验证(手机号、身份证号、邮箱、车牌号、银行卡号)
- ✅ 收货地址解析
- ✅ 字符串格式化和转换
- ✅ 字符串相似度比较
- ✅ 多分隔符字符串分割
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
maskPhone() |
手机号脱敏 | Str::maskPhone('13800138000') |
maskEmail() |
邮箱脱敏 | Str::maskEmail('user@example.com') |
camel() |
转驼峰命名 | Str::camel('hello_world') |
snake() |
转蛇形命名 | Str::snake('helloWorld') |
toBase64() |
转Base64编码 | Str::toBase64('hello') |
fromBase64() |
Base64解码 | Str::fromBase64('aGVsbG8=') |
使用示例
脱敏
mask() - 字符串脱敏
$masked = Str::mask('13800138000', 3, 4, '*'); // '138****8000' $masked = Str::mask('user@example.com', 2, 4, '*'); // 'us****@example.com'
maskPhone() - 手机号脱敏
$phone = Str::maskPhone('13800138000'); // '138****8000' $phone = Str::maskPhone('13800138000', 3, 4); // '138****8000'
maskEmail() - 邮箱脱敏
$email = Str::maskEmail('user@example.com'); // 'us***@example.com' $email = Str::maskEmail('user@example.com', 2, 3); // 'us***@example.com'
maskIdCard() - 身份证号脱敏
$idCard = Str::maskIdCard('110101199001011234'); // '110101********1234' $idCard = Str::maskIdCard('110101199001011234', 6, 8); // '110101********1234'
maskBankCard() - 银行卡号脱敏
$bankCard = Str::maskBankCard('6222021234567890123'); // '622202********0123' $bankCard = Str::maskBankCard('6222021234567890123', 6, 10); // '622202********0123'
maskName() - 姓名脱敏
$name = Str::maskName('张三'); // '张*' $name = Str::maskName('欧阳修'); // '欧阳*' $name = Str::maskName('张三丰', 1, 1); // '张*丰'
字符串长度和截断
length() - 字符串长度
$length = Str::length('你好世界'); // 4(使用mb_strlen)
truncate() - 字符串截断
$truncated = Str::truncate('这是一段很长的文本内容', 10); // '这是一段很长的文...' $truncated = Str::truncate('这是一段很长的文本内容', 10, '---'); // '这是一段很长的文---'
limit() - 字符串限制长度
$limited = Str::limit('这是一段很长的文本内容', 10); // '这是一段很长的文...' $limited = Str::limit('短文本', 10); // '短文本'(不超过长度不截断)
wordTruncate() - 字符串单词截断
$wordTruncate = Str::wordTruncate('This is a long text content', 10); // 'This is a...'
字符串命名转换
snake() - 驼峰转下划线
$snake = Str::snake('helloWorld'); // 'hello_world' $snake = Str::snake('HelloWorld'); // 'hello_world' $snake = Str::snake('HelloWorld', '-'); // 'hello-world'
toSnake() - 驼峰命名转下划线命名(支持数组键名)
$snake = Str::toSnake('helloWorld'); // 'hello_world' $snake = Str::toSnake('HelloWorld'); // 'hello_world' $snake = Str::toSnake(['AaBbCc'=>1]); // ['aa_bb_cc'=>1]
camel() - 下划线转驼峰
$camel = Str::camel('hello_world'); // 'helloWorld' $camel = Str::camel('hello-world'); // 'helloWorld' $camel = Str::camel('hello_world', '-'); // 'helloWorld'
toCamel() - 下划线命名转驼峰命名(支持数组键名)
$camel = Str::toCamel('hello_world', false); // 'helloWorld' $camel = Str::toCamel('hello_world', true); // 'HelloWorld' $camel = Str::toCamel(['aa_bb_cc'=>1], false); // 'aaBbCc' $camel = Str::toCamel(['aa_bb_cc'=>1], true); // 'AaBbCc'
studly() - 首字母大写驼峰命名
$camel = Str::studly('hello_world'); // 'HelloWorld' $camel = Str::studly('hello-world'); // 'HelloWorld' $camel = Str::studly('hello.world'); // 'HelloWorld' $camel = Str::studly('aa-bb_cc.dd'); // 'AaBbCcDd'
ucfirst() - 首字母大写
$ucfirst = Str::ucfirst('hello'); // 'Hello'
lcfirst() - 首字母小写
$lcfirst = Str::lcfirst('Hello'); // 'hello'
ucwords() - 单词首字母大写
$ucwords = Str::ucwords('hello world'); // 'Hello World'
upper() - 全部大写
$upper = Str::upper('hello'); // 'HELLO'
lower() - 全部小写
$lower = Str::lower('HELLO'); // 'hello'
swap() - 大小写转换
$swap = Str::swap('Hello'); // 'hELLO'
title() - 标题格式
$title = Str::title('hello world'); // 'Hello World'
字符串查找和判断
contains() - 是否包含子串
$contains = Str::contains('hello world', 'world'); // true $contains = Str::contains('hello world', 'php'); // false
startsWith() - 是否以子串开头
$startsWith = Str::startsWith('hello world', 'hello'); // true $startsWith = Str::startsWith('hello world', 'world'); // false
endsWith() - 是否以子串结尾
$endsWith = Str::endsWith('hello world', 'world'); // true $endsWith = Str::endsWith('hello world', 'hello'); // false
pos() - 子串首次出现位置
$pos = Str::pos('hello world', 'world'); // 6 $pos = Str::pos('hello world', 'php'); // false
rpos() - 子串最后一次出现位置
$pos = Str::rpos('hello world world', 'world'); // 12 $pos = Str::rpos('hello world', 'php'); // false
count() - 子串出现次数
$count = Str::count('hello world world', 'world'); // 2
match() - 字符串是否匹配正则
$match = Str::match('hello123', '/^[a-z]+\d+$/'); // true $match = Str::match('hello', '/^[a-z]+\d+$/'); // false
字符串替换
replace() - 字符串替换
$replaced = Str::replace('hello world', 'world', 'php'); // 'hello php'
replaceArray() - 字符串批量替换
$replaced = Str::replaceArray('hello world', ['hello' => 'hi', 'world' => 'php']); // 'hi php'
replaceRegex() - 字符串正则替换
$replaced = Str::replaceRegex('hello123world', '/\d+/', '456'); // 'hello456world'
substrReplace() - 字符串截取并替换
$replaced = Str::substrReplace('hello world', 'php', 6, 5); // 'hello php'
字符串分割和连接
split() - 字符串分割
$parts = Str::split('hello,world,php', ','); // ['hello', 'world', 'php']
toArray() - 字符串分割为数组
$array = Str::toArray('hello,world,php', ','); // ['hello', 'world', 'php']
fromArray() - 数组连接为字符串
$string = Str::fromArray(['hello', 'world', 'php'], ','); // 'hello,world,php'
join() - 字符串连接
$joined = Str::join(['hello', 'world', 'php'], ','); // 'hello,world,php'
concat() - 字符串拼接
$concat = Str::concat('hello', ' ', 'world'); // 'hello world'
字符串去除空白
trim() - 去除首尾空白
$trimmed = Str::trim(' hello world '); // 'hello world'
ltrim() - 去除左侧空白
$ltrim = Str::ltrim(' hello world '); // 'hello world '
rtrim() - 去除右侧空白
$rtrim = Str::rtrim(' hello world '); // ' hello world'
clean() - 去除所有空白
$clean = Str::clean(' hello world '); // 'helloworld'
字符串填充
padLeft() - 左侧填充
$padded = Str::padLeft('123', 6, '0'); // '000123'
padRight() - 右侧填充
$padded = Str::padRight('123', 6, '0'); // '123000'
padBoth() - 两侧填充
$padded = Str::padBoth('123', 7, '*'); // '**123**'
字符串重复
repeat() - 字符串重复
$repeated = Str::repeat('hello', 3); // 'hellohellohello'
reverse() - 字符串反转
$reversed = Str::reverse('hello'); // 'olleh'
字符串随机
random() - 生成随机字符串
$random = Str::random(16); // 16位随机字符串
numeric() - 生成随机数字字符串
$numeric = Str::numeric(6); // 6位随机数字字符串
alpha() - 生成随机字母字符串
$alpha = Str::alpha(8); // 8位随机字母字符串
字符串编码解码
toBase64() - Base64编码
$base64 = Str::toBase64('hello'); // 'aGVsbG8='
fromBase64() - Base64解码
$decoded = Str::fromBase64('aGVsbG8='); // 'hello'
toUrlEncode() - URL编码
$urlEncoded = Str::toUrlEncode('hello world'); // 'hello%20world'
fromUrlEncode() - URL解码
$urlDecoded = Str::fromUrlEncode('hello%20world'); // 'hello world'
toHtmlEntities() - HTML实体编码
$htmlEncoded = Str::toHtmlEntities('<div>hello</div>'); // '<div>hello</div>'
fromHtmlEntities() - HTML实体解码
$htmlDecoded = Str::fromHtmlEntities('<div>hello</div>'); // '<div>hello</div>'
toJson() - JSON编码
$json = Str::toJson(['name' => '张三', 'age' => 25]); // '{"name":"张三","age":25}'
fromJson() - JSON解码
$decoded = Str::fromJson('{"name":"张三","age":25}'); // ['name' => '张三', 'age' => 25]
toXml() - XML编码
$xml = Str::toXml(['name' => '张三', 'age' => 25], 'root'); // '<root><name>张三</name><age>25</age></root>'
fromXml() - XML解码
$decoded = Str::fromXml('<root><name>张三</name><age>25</age></root>'); // ['name' => '张三', 'age' => 25]
toBinary() - 二进制编码
$binary = Str::toBinary('hello'); // '0110100001100101011011000110110001101111'
fromBinary() - 二进制解码
$decoded = Str::fromBinary('0110100001100101011011000110110001101111'); // 'hello'
toHex() - 十六进制编码
$hex = Str::toHex('hello'); // '68656c6c6f'
fromHex() - 十六进制解码
$decoded = Str::fromHex('68656c6c6f'); // 'hello'
字符串哈希
md5() - MD5哈希
$md5 = Str::md5('hello'); // '5d41402abc4b2a76b9719d911017c592'
sha1() - SHA1哈希
$sha1 = Str::sha1('hello'); // 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
sha256() - SHA256哈希
$sha256 = Str::sha256('hello'); // '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
sha512() - SHA512哈希
$sha512 = Str::sha512('hello'); // '9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043'
字符串验证
isEmail() - 是否为邮箱
$isEmail = Str::isEmail('user@example.com'); // true $isEmail = Str::isEmail('invalid-email'); // false
isUrl() - 是否为URL
$isUrl = Str::isUrl('https://example.com'); // true $isUrl = Str::isUrl('not-a-url'); // false
isIp() - 是否为IP地址
$isIp = Str::isIp('192.168.1.1'); // true $isIp = Str::isIp('not-an-ip'); // false
isIpv4() - 是否为IPv4地址
$isIpv4 = Str::isIpv4('192.168.1.1'); // true $isIpv4 = Str::isIpv4('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // false
isIpv6() - 是否为IPv6地址
$isIpv6 = Str::isIpv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // true $isIpv6 = Str::isIpv6('192.168.1.1'); // false
isPhone() - 是否为手机号
$isPhone = Str::isPhone('13800138000'); // true $isPhone = Str::isPhone('12345678901'); // false
isIdCard() - 是否为身份证号
$isIdCard = Str::isIdCard('110101199001011234'); // true $isIdCard = Str::isIdCard('123456789012345678'); // false
isBankCard() - 是否为银行卡号
$isBankCard = Str::isBankCard('6222021234567890123'); // true $isBankCard = Str::isBankCard('1234567890'); // false
isNumeric() - 是否为数字
$isNumeric = Str::isNumeric('12345'); // true $isNumeric = Str::isNumeric('abc123'); // false
isAlpha() - 是否为字母
$isAlpha = Str::isAlpha('hello'); // true $isAlpha = Str::isAlpha('hello123'); // false
isAlnum() - 是否为字母数字
$isAlnum = Str::isAlnum('hello123'); // true $isAlnum = Str::isAlnum('hello world'); // false
isHex() - 是否为十六进制
$isHex = Str::isHex('1a2b3c'); // true $isHex = Str::isHex('1g2h3i'); // false
isBinary() - 是否为二进制
$isBinary = Str::isBinary('01010101'); // true $isBinary = Str::isBinary('12345678'); // false
isJson() - 是否为JSON
$isJson = Str::isJson('{"name":"张三"}'); // true $isJson = Str::isJson('not json'); // false
isXml() - 是否为XML
$isXml = Str::isXml('<root>hello</root>'); // true $isXml = Str::isXml('not xml'); // false
isSerialized() - 是否为序列化数据
$isSerialized = Str::isSerialized('a:1:{i:0;s:5:"hello";}'); // true $isSerialized = Str::isSerialized('not serialized'); // false
isBase64() - 是否为Base64
$isBase64 = Str::isBase64('aGVsbG8='); // true $isBase64 = Str::isBase64('not base64'); // false
字符串转换
toArray() - 字符串转数组
$array = Str::toArray('hello,world,php', ','); // ['hello', 'world', 'php']
fromArray() - 数组转字符串
$string = Str::fromArray(['hello', 'world', 'php'], ','); // 'hello,world,php'
toObject() - 字符串转对象
$object = Str::toObject('{"name":"张三","age":25}'); // stdClass Object ( [name] => 张三 [age] => 25 )
fromObject() - 对象转字符串
$string = Str::fromObject((object)['name' => '张三', 'age' => 25]); // '{"name":"张三","age":25}'
toQuery() - 字符串转查询字符串
$query = Str::toQuery(['name' => '张三', 'age' => 25]); // 'name=%E5%BC%A0%E4%B8%89&age=25'
fromQuery() - 查询字符串转数组
$array = Str::fromQuery('name=%E5%BC%A0%E4%B8%89&age=25'); // ['name' => '张三', 'age' => 25]
字符串格式化
format() - 字符串格式化
$formatted = Str::format('Hello, %s! You are %d years old.', '张三', 25); // 'Hello, 张三! You are 25 years old.'
template() - 字符串模板渲染
$rendered = Str::template('Hello, {{name}}! Age: {{age}}', ['name' => '张三', 'age' => 25]); // 'Hello, 张三! Age: 25'
indent() - 字符串缩进
$indented = Str::indent('hello', 4); // ' hello'
unindent() - 字符串去除缩进
$unindented = Str::unindent(' hello'); // 'hello'
字符串字符操作
first() - 获取字符串首字符
$first = Str::first('hello'); // 'h'
last() - 获取字符串尾字符
$last = Str::last('hello'); // 'o'
firstN() - 获取字符串前N个字符
$firstN = Str::firstN('hello', 2); // 'he'
lastN() - 获取字符串后N个字符
$lastN = Str::lastN('hello', 2); // 'lo'
removeFirst() - 去除首字符
$removed = Str::removeFirst('hello'); // 'ello'
removeLast() - 去除尾字符
$removed = Str::removeLast('hello'); // 'hell'
removeFirstN() - 去除前N个字符
$removed = Str::removeFirstN('hello', 2); // 'llo'
removeLastN() - 去除后N个字符
$removed = Str::removeLastN('hello', 2); // 'hel'
字符串统计
count() - 统计子串出现次数
$count = Str::count('hello world world', 'world'); // 2
wordCount() - 统计单词数
$wordCount = Str::wordCount('hello world php'); // 3
charCount() - 统计字符数
$charCount = Str::charCount('hello'); // 5
byteLength() - 统计字节长度
$byteLength = Str::byteLength('你好'); // 6(UTF-8编码)
字符串安全
escapeHtml() - 转义HTML特殊字符
$escaped = Str::escapeHtml('<div>hello</div>'); // '<div>hello</div>'
escapeSql() - 转义SQL特殊字符
$escaped = Str::escapeSql("O'Reilly"); // "O\'Reilly"
escapeJs() - 转义JavaScript特殊字符
$escaped = Str::escapeJs("It's a test"); // "It\'s a test"
escapeRegex() - 转义正则表达式特殊字符
$escaped = Str::escapeRegex('hello.world'); // 'hello\.world'
字符串比较
compare() - 字符串比较
$compare = Str::compare('hello', 'hello'); // 0(相等) $compare = Str::compare('hello', 'world'); // -15(不相等)
similarity() - 字符串相似度
$similarity = Str::similarity('hello', 'hello'); // 1.0(完全相同) $similarity = Str::similarity('hello', 'world'); // 0.2(相似度)
distance() - 字符串编辑距离
$distance = Str::distance('hello', 'hello'); // 0(相同) $distance = Str::distance('hello', 'world'); // 4(编辑距离)
时间处理模块
特性
- ✅ 时间格式化
- ✅ 时间计算(加法、减法、差值)
- ✅ 常用时间获取(当前时间、今天、昨天、明天)
- ✅ 时区支持
- ✅ 人性化时间显示、日期范围计算
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
format() |
格式化时间 | Time::format(time(), 'Y-m-d') |
now() |
获取当前时间 | Time::now() |
today() |
获取今天日期 | Time::today() |
yesterday() |
获取昨天日期 | Time::yesterday() |
tomorrow() |
获取明天日期 | Time::tomorrow() |
add() |
时间加法 | Time::add(time(), 3600) |
sub() |
时间减法 | Time::sub(time(), 3600) |
diff() |
时间差 | Time::diff(time(), time() - 3600) |
diffForHumans() |
人性化时间差 | Time::diffForHumans(time() - 3600) |
weekStart() |
本周开始时间 | Time::weekStart() |
weekEnd() |
本周结束时间 | Time::weekEnd() |
monthStart() |
本月开始时间 | Time::monthStart() |
monthEnd() |
本月结束时间 | Time::monthEnd() |
使用示例
时间格式化
format() - 格式化时间
$formatted = Time::format(time(), 'Y-m-d H:i:s'); // '2025-12-26 12:00:00' $formatted = Time::format(time(), 'Y年m月d日'); // '2025年12月26日' $formatted = Time::format(1735200000, 'Y-m-d'); // '2025-12-26'
now() - 获取当前时间
$now = Time::now(); // '2025-12-26 12:00:00' $now = Time::now('Y-m-d'); // '2025-12-26' $now = Time::now('H:i:s'); // '12:00:00'
today() - 获取今天日期
$today = Time::today(); // '2025-12-26' $today = Time::today('Y-m-d H:i:s'); // '2025-12-26 00:00:00'
yesterday() - 获取昨天日期
$yesterday = Time::yesterday(); // '2025-12-25' $yesterday = Time::yesterday('Y-m-d H:i:s'); // '2025-12-25 00:00:00'
tomorrow() - 获取明天日期
$tomorrow = Time::tomorrow(); // '2025-12-27' $tomorrow = Time::tomorrow('Y-m-d H:i:s'); // '2025-12-27 00:00:00'
时间计算
$timestamp = time();
add() - 时间加法
$newTime = Time::add($timestamp, 3600); // 加1小时 $newTime = Time::add($timestamp, 86400); // 加1天 $newTime = Time::add($timestamp, 604800); // 加1周
sub() - 时间减法
$newTime = Time::sub($timestamp, 3600); // 减1小时 $newTime = Time::sub($timestamp, 86400); // 减1天 $newTime = Time::sub($timestamp, 604800); // 减1周
diff() - 时间差
$diff = Time::diff(time(), time() - 3600); // 3600(秒) $diff = Time::diff(time() - 86400, time()); // 86400(秒)
diffForHumans() - 人性化时间差
$human = Time::diffForHumans(time() - 60); // '1分钟前' $human = Time::diffForHumans(time() - 3600); // '1小时前' $human = Time::diffForHumans(time() - 86400); // '1天前' $human = Time::diffForHumans(time() - 2592000); // '1个月前' $human = Time::diffForHumans(time() - 31536000); // '1年前' $human = Time::diffForHumans(time() + 3600); // '1小时后' $human = Time::diffForHumans(time() + 86400); // '1天后' // 指定基准时间的人性化时间差 $human = Time::diffForHumans(time() - 3600, time() - 7200); // '1小时前'
周日期范围
weekStart() - 获取本周开始时间
$weekStart = Time::weekStart(); // 本周一00:00:00的时间戳 $weekStart = Time::weekStart(time()); // 指定时间所在周的开始时间 $weekStartFormatted = Time::format(Time::weekStart(), 'Y-m-d H:i:s'); // '2025-12-22 00:00:00'
weekEnd() - 获取本周结束时间
$weekEnd = Time::weekEnd(); // 本周日23:59:59的时间戳 $weekEnd = Time::weekEnd(time()); // 指定时间所在周的结束时间 $weekEndFormatted = Time::format(Time::weekEnd(), 'Y-m-d H:i:s'); // '2025-12-28 23:59:59'
lastWeekStart() - 获取上周开始时间
$lastWeekStart = Time::lastWeekStart(); // 上周一00:00:00的时间戳 $lastWeekStartFormatted = Time::format(Time::lastWeekStart(), 'Y-m-d H:i:s'); // '2025-12-15 00:00:00'
lastWeekEnd() - 获取上周结束时间
$lastWeekEnd = Time::lastWeekEnd(); // 上周日23:59:59的时间戳 $lastWeekEndFormatted = Time::format(Time::lastWeekEnd(), 'Y-m-d H:i:s'); // '2025-12-21 23:59:59'
月日期范围
monthStart() - 获取本月开始时间
$monthStart = Time::monthStart(); // 本月1日00:00:00的时间戳 $monthStart = Time::monthStart(time()); // 指定时间所在月的开始时间 $monthStartFormatted = Time::format(Time::monthStart(), 'Y-m-d H:i:s'); // '2025-12-01 00:00:00'
monthEnd() - 获取本月结束时间
$monthEnd = Time::monthEnd(); // 本月最后一天23:59:59的时间戳 $monthEnd = Time::monthEnd(time()); // 指定时间所在月的结束时间 $monthEndFormatted = Time::format(Time::monthEnd(), 'Y-m-d H:i:s'); // '2025-12-31 23:59:59'
lastMonthStart() - 获取上月开始时间
$lastMonthStart = Time::lastMonthStart(); // 上月1日00:00:00的时间戳 $lastMonthStartFormatted = Time::format(Time::lastMonthStart(), 'Y-m-d H:i:s'); // '2025-11-01 00:00:00'
lastMonthEnd() - 获取上月结束时间
$lastMonthEnd = Time::lastMonthEnd(); // 上月最后一天23:59:59的时间戳 $lastMonthEndFormatted = Time::format(Time::lastMonthEnd(), 'Y-m-d H:i:s'); // '2025-11-30 23:59:59'
年日期范围
yearStart() - 获取本年开始时间
$yearStart = Time::yearStart(); // 本年1月1日00:00:00的时间戳 $yearStart = Time::yearStart(time()); // 指定时间所在年的开始时间 $yearStartFormatted = Time::format(Time::yearStart(), 'Y-m-d H:i:s'); // '2025-01-01 00:00:00'
yearEnd() - 获取本年结束时间
$yearEnd = Time::yearEnd(); // 本年12月31日23:59:59的时间戳 $yearEnd = Time::yearEnd(time()); // 指定时间所在年的结束时间 $yearEndFormatted = Time::format(Time::yearEnd(), 'Y-m-d H:i:s'); // '2025-12-31 23:59:59'
lastYearStart() - 获取上年开始时间
$lastYearStart = Time::lastYearStart(); // 上年1月1日00:00:00的时间戳 $lastYearStartFormatted = Time::format(Time::lastYearStart(), 'Y-m-d H:i:s'); // '2024-01-01 00:00:00'
lastYearEnd() - 获取上年结束时间
$lastYearEnd = Time::lastYearEnd(); // 上年12月31日23:59:59的时间戳 $lastYearEndFormatted = Time::format(Time::lastYearEnd(), 'Y-m-d H:i:s'); // '2024-12-31 23:59:59'
时间判断
$timestamp = time();
between() - 判断是否在某个时间区间内
$inRange = Time::between($timestamp, time() - 3600, time() + 3600); // true $inRange = Time::between(time() - 7200, time() - 3600, time()); // false
isToday() - 判断是否是今天
$isToday = Time::isToday($timestamp); // true $isToday = Time::isToday(time() - 86400); // false
isYesterday() - 判断是否是昨天
$isYesterday = Time::isYesterday(time() - 86400); // true $isYesterday = Time::isYesterday(time()); // false
isTomorrow() - 判断是否是明天
$isTomorrow = Time::isTomorrow(time() + 86400); // true $isTomorrow = Time::isTomorrow(time()); // false
isThisWeek() - 判断是否是本周
$isThisWeek = Time::isThisWeek($timestamp); // true $isThisWeek = Time::isThisWeek(time() - 604800); // false
isThisMonth() - 判断是否是本月
$isThisMonth = Time::isThisMonth($timestamp); // true $isThisMonth = Time::isThisMonth(strtotime('2025-11-01')); // false
isThisYear() - 判断是否是本年
$isThisYear = Time::isThisYear($timestamp); // true $isThisYear = Time::isThisYear(strtotime('2024-01-01')); // false
日期信息获取
daysInMonth() - 获取某个月的天数
$days = Time::daysInMonth(12); // 31(12月有31天) $days = Time::daysInMonth(2, 2024); // 29(2024年2月有29天,闰年) $days = Time::daysInMonth(2, 2023); // 28(2023年2月有28天,平年)
dayOfWeek() - 获取某天是周几
$weekday = Time::dayOfWeek(time()); // 0-6(0表示周日,1表示周一,以此类推) $weekday = Time::dayOfWeek(strtotime('2025-12-26')); // 5(周五)
dayOfWeekName() - 获取某天是周几(中文名称)
$weekdayName = Time::dayOfWeekName(time()); // '周五' $weekdayName = Time::dayOfWeekName(strtotime('2025-12-28')); // '周日'
dayOfYear() - 获取某天是本年第几天
$dayOfYear = Time::dayOfYear(time()); // 360(2025年第360天) $dayOfYear = Time::dayOfYear(strtotime('2025-01-01')); // 1(第1天)
weekOfYear() - 获取某天是本年第几周
$weekOfYear = Time::weekOfYear(time()); // 52(第52周) $weekOfYear = Time::weekOfYear(strtotime('2025-01-01')); // 1(第1周)
年龄计算
age() - 计算年龄
$age = Time::age('1990-01-01'); // 35(假设当前是2025年) $age = Time::age('2000-12-31'); // 24(假设当前是2025年) $age = Time::age('2010-06-15'); // 15(假设当前是2025年)
时间戳转换
toTimestamp() - 时间字符串转时间戳
$timestamp = Time::toTimestamp('2025-12-26 12:00:00'); // 1735200000 $timestamp = Time::toTimestamp('2025-12-26'); // 1735152000 $timestamp = Time::toTimestamp('now'); // 当前时间戳 $timestamp = Time::toTimestamp('+1 day'); // 明天同一时间的时间戳
toMillisecond() - 时间戳转毫秒
$millisecond = Time::toMillisecond(time()); // 1735200000000 $millisecond = Time::toMillisecond(1735200000); // 1735200000000
fromMillisecond() - 毫秒转时间戳
$timestamp = Time::fromMillisecond(1735200000000); // 1735200000
millisecond() - 获取当前毫秒时间戳
$millisecond = Time::millisecond(); // 当前时间的毫秒时间戳
microsecond() - 获取当前微秒时间戳
$microsecond = Time::microsecond(); // 当前时间的微秒时间戳
microtime() - 获取当前时间戳(带微秒)
$microtime = Time::microtime(); // 1735200000.123456
时区操作
timezone() - 获取当前时区
$timezone = Time::timezone(); // 'Asia/Shanghai'(或其他时区)
setTimezone() - 设置时区
$success = Time::setTimezone('UTC'); // true $success = Time::setTimezone('America/New_York'); // true // 设置时区后获取时间 Time::setTimezone('UTC'); $utcTime = Time::now(); // UTC时间 Time::setTimezone('Asia/Shanghai'); $shanghaiTime = Time::now(); // 上海时间
实际应用场景
// 订单创建时间显示 $createdAt = time() - 3600; // 1小时前创建 $displayTime = Time::diffForHumans($createdAt); // '1小时前' // 数据统计时间范围 $weekStart = Time::weekStart(); $weekEnd = Time::weekEnd(); // 查询本周数据:WHERE created_at >= $weekStart AND created_at <= $weekEnd // 用户生日计算 $birthday = '1990-05-15'; $age = Time::age($birthday); // 35岁 // 活动倒计时 $endTime = strtotime('2025-12-31 23:59:59'); $diff = Time::diff(time(), $endTime); // 距离结束的秒数 // 日程安排 $today = Time::today(); $tomorrow = Time::tomorrow(); $weekStart = Time::weekStart(); $weekEnd = Time::weekEnd(); // 数据归档 $lastMonthStart = Time::lastMonthStart(); $lastMonthEnd = Time::lastMonthEnd(); // 归档上月数据:WHERE created_at >= $lastMonthStart AND created_at <= $lastMonthEnd // 报表生成 $yearStart = Time::yearStart(); $yearEnd = Time::yearEnd(); // 生成本年报表:WHERE created_at >= $yearStart AND created_at <= $yearEnd // 定时任务检查 if (Time::isToday($taskTime)) { // 执行今天的任务 } // 工作日判断 $weekday = Time::dayOfWeek(time()); if ($weekday >= 1 && $weekday <= 5) { // 周一到周五,工作日 } else { // 周六周日,休息日 } // 高精度时间测量 $startTime = Time::microsecond(); // 执行一些代码 $endTime = Time::microsecond(); $duration = $endTime - $startTime; // 微秒
数学计算处理模块
特性
- ✅ 高精度计算(使用bcmath扩展)
- ✅ 支持金融计算(折扣、税费、利息)
- ✅ 支持统计分析(平均数、中位数、标准差)
- ✅ 支持幂运算、平方根等数学操作
- ✅ 结果保留任意小数位数
- ✅ 兼容PHP8.3+特性
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
add() |
高精度加法 | Math::add('1.1', '2.2') |
sub() |
高精度减法 | Math::sub('3.3', '1.1') |
mul() |
高精度乘法 | Math::mul('2.5', '4') |
div() |
高精度除法 | Math::div('10', '3') |
avg() |
平均数 | Math::avg([1, 2, 3, 4, 5]) |
median() |
中位数 | Math::median([1, 2, 3, 4, 5]) |
discount() |
计算折扣价 | Math::discount('100', '0.2') |
tax() |
计算税额 | Math::tax('100', '0.13') |
使用示例
基础运算
add() - 高精度加法
$sum = Math::add(0.1, 0.2); // 0.3(而不是0.30000000000000004) $sum = Math::add('1.1', '2.2', 2); // 3.30(保留2位小数)
sub() - 高精度减法
$diff = Math::sub(0.3, 0.1); // 0.2 $diff = Math::sub('5.5', '2.2', 2); // 3.30
mul() - 高精度乘法
$product = Math::mul(0.1, 0.2); // 0.02 $product = Math::mul('1.5', '2.5', 2); // 3.75
div() - 高精度除法
$quotient = Math::div(0.3, 0.1); // 3 $quotient = Math::div('10', '3', 2); // 3.33
mod() - 取模运算
$mod = Math::mod(10, 3); // 1 $mod = Math::mod('10.5', '3'); // 1.5
pow() - 幂运算
$pow = Math::pow(2, 10); // 1024 $pow = Math::pow('2.5', 3, 2); // 15.62
sqrt() - 平方根运算
$sqrt = Math::sqrt(16); // 4 $sqrt = Math::sqrt('2', 4); // 1.4142
取整运算
round() - 四舍五入
$rounded = Math::round(3.14159, 2); // 3.14 $rounded = Math::round(3.5, 0); // 4
ceil() - 向上取整
$ceil = Math::ceil(3.2); // 4 $ceil = Math::ceil(3.8, 1); // 3.8 $ceil = Math::ceil('3.21', 1); // 3.3
floor() - 向下取整
$floor = Math::floor(3.8); // 3 $floor = Math::floor(3.2, 1); // 3.2 $floor = Math::floor('3.89', 1); // 3.8
比较运算
compare() - 比较两个数的大小
$result = Math::compare(5, 3); // 1(5 > 3) $result = Math::compare(3, 5); // -1(3 < 5) $result = Math::compare(5, 5); // 0(相等)
equal() - 判断两个数是否相等
$equal = Math::equal(0.1 + 0.2, 0.3); // true(解决浮点数比较问题) $equal = Math::equal('1.000', '1.00', 2); // true(保留2位小数比较)
格式化
format() - 格式化数字
$formatted = Math::format(1234567.89, 2, true); // 1,234,567.89(带千分位) $formatted = Math::format(1234567.89, 2, false); // 1234567.89(不带千分位) $formatted = Math::format('1234567.89123', 3, true); // 1,234,567.891
三角函数
sin() - 正弦函数
$sin = Math::sin(Math::deg2rad(30)); // 0.5(30度的正弦值) $sin = Math::sin(3.14159 / 2); // 1(π/2的正弦值)
cos() - 余弦函数
$cos = Math::cos(Math::deg2rad(60)); // 0.5(60度的余弦值) $cos = Math::cos(0); // 1(0度的余弦值)
tan() - 正切函数
$tan = Math::tan(Math::deg2rad(45)); // 1(45度的正切值) $tan = Math::tan(3.14159 / 4); // 1(π/4的正切值)
asin() - 反正弦函数
$asin = Math::asin(1); // 1.5708(π/2) $asin = Math::asin(0.5); // 0.5236(π/6)
acos() - 反余弦函数
$acos = Math::acos(0); // 1.5708(π/2) $acos = Math::acos(0.5); // 1.0472(π/3)
atan() - 反正切函数
$atan = Math::atan(1); // 0.7854(π/4) $atan = Math::atan(0); // 0
对数运算
ln() - 自然对数(以e为底)
$ln = Math::ln(Math::exp(1)); // 1 $ln = Math::ln(2.71828); // 1
log10() - 常用对数(以10为底)
$log10 = Math::log10(100); // 2 $log10 = Math::log10(1000); // 3
log() - 自定义底数对数
$log = Math::log(8, 2); // 3(2的3次方等于8) $log = Math::log(100, 10); // 2(10的2次方等于100)
角度转换
rad2deg() - 弧度转角度
$deg = Math::rad2deg(3.14159); // 180(π弧度 = 180度) $deg = Math::rad2deg(1.5708); // 90(π/2弧度 = 90度)
deg2rad() - 角度转弧度
$rad = Math::deg2rad(180); // 3.14159(180度 = π弧度) $rad = Math::deg2rad(90); // 1.5708(90度 = π/2弧度)
数值操作
abs() - 绝对值
$abs = Math::abs(-10); // 10 $abs = Math::abs(10); // 10 $abs = Math::abs('-5.5'); // 5.5
factorial() - 阶乘
$factorial = Math::factorial(5); // 120(5! = 5×4×3×2×1) $factorial = Math::factorial(0); // 1(0! = 1)
gcd() - 最大公约数
$gcd = Math::gcd(12, 18); // 6 $gcd = Math::gcd(24, 36); // 12
lcm() - 最小公倍数
$lcm = Math::lcm(4, 6); // 12 $lcm = Math::lcm(3, 5); // 15
金融计算
percentage() - 百分比计算
$percentage = Math::percentage(25, 100); // 25(25占100的25%) $percentage = Math::percentage('30', '150', 2); // 20.00
discount() - 折扣计算
$discounted = Math::discount(100, 0.8); // 80(打8折) $discounted = Math::discount('200', '0.7', 2); // 140.00(打7折)
tax() - 税费计算
$tax = Math::tax(100, 0.1); // 10(100的10%税额) $tax = Math::tax('500', '0.13', 2); // 65.00(500的13%税额)
taxIncluded() - 含税金额计算
$taxIncluded = Math::taxIncluded(100, 0.1); // 110(100 + 10%税) $taxIncluded = Math::taxIncluded('500', '0.13', 2); // 565.00
taxExcluded() - 不含税金额计算
$taxExcluded = Math::taxExcluded(110, 0.1); // 100(110 / 1.1) $taxExcluded = Math::taxExcluded('565', '0.13', 2); // 500.00
simpleInterest() - 简单利息计算
$simpleInterest = Math::simpleInterest(1000, 0.05, 2); // 100(1000本金,5%年利率,2年) $simpleInterest = Math::simpleInterest('5000', '0.04', 3, 2); // 600.00
compoundInterest() - 复利计算
$compoundInterest = Math::compoundInterest(1000, 0.05, 2); // 1102.5(1000本金,5%年利率,2年复利) $compoundInterest = Math::compoundInterest('5000', '0.04', 3, 2); // 5624.32
随机数生成
random() - 生成指定范围内的随机数
$random = Math::random(1, 100); // 1-100之间的随机整数 $random = Math::random('1.5', '5.5', 2); // 1.50-5.50之间的随机数(保留2位小数)
范围检查
inRange() - 数值范围检查
$inRange = Math::inRange(5, 1, 10); // true(5在1-10范围内) $inRange = Math::inRange(15, 1, 10); // false(15不在1-10范围内)
clamp() - 限制数值范围
$clamped = Math::clamp(5, 1, 10); // 5(在范围内,保持不变) $clamped = Math::clamp(15, 1, 10); // 10(超出最大值,限制为10) $clamped = Math::clamp(-5, 1, 10); // 1(小于最小值,限制为1) $clamped = Math::clamp('5.5', '1.0', '10.0', 1); // 5.5
数值判断
isPositive() - 判断是否为正数
$isPositive = Math::isPositive(10); // true $isPositive = Math::isPositive(-5); // false $isPositive = Math::isPositive(0); // false
isNegative() - 判断是否为负数
$isNegative = Math::isNegative(-5); // true $isNegative = Math::isNegative(10); // false $isNegative = Math::isNegative(0); // false
isZero() - 判断是否为零
$isZero = Math::isZero(0); // true $isZero = Math::isZero(0.0000000001, 10); // true(保留10位小数比较) $isZero = Math::isZero(1); // false
isEven() - 判断是否为偶数
$isEven = Math::isEven(4); // true $isEven = Math::isEven(5); // false
isOdd() - 判断是否为奇数
$isOdd = Math::isOdd(5); // true $isOdd = Math::isOdd(4); // false
isPrime() - 判断是否为质数
$isPrime = Math::isPrime(7); // true $isPrime = Math::isPrime(4); // false $isPrime = Math::isPrime(2); // true $isPrime = Math::isPrime(1); // false
isValid() - 判断数值是否有效
$isValid = Math::isValid(123); // true $isValid = Math::isValid('123.45'); // true $isValid = Math::isValid('abc'); // false $isValid = Math::isValid(INF); // false(无穷大) $isValid = Math::isValid(NAN); // false(非数字)
插值运算
lerp() - 线性插值
$lerp = Math::lerp(0, 10, 0.5); // 5(0和10的中点) $lerp = Math::lerp(0, 10, 0.25); // 2.5(0和10的25%位置) $lerp = Math::lerp('0', '100', '0.75', 1); // 75.0
统计分析
average() - 平均值计算
$avg = Math::average([1, 2, 3, 4, 5]); // 3 $avg = Math::average([1.5, 2.5, 3.5], 2); // 2.50
median() - 中位数计算
$median = Math::median([1, 2, 3, 4, 5]); // 3 $median = Math::median([1, 2, 3, 4]); // 2.5(2和3的平均值) $median = Math::median(['1.5', '2.5', '3.5'], 2); // 2.50
mode() - 众数计算
$mode = Math::mode([1, 2, 2, 3, 3, 3]); // 3(出现次数最多) $mode = Math::mode([1, 2, 3]); // 1(多个众数时返回第一个)
standardDeviation() - 标准差计算
$stdDev = Math::standardDeviation([1, 2, 3, 4, 5]); // 1.414 $stdDev = Math::standardDeviation([10, 20, 30], 2); // 10.00
实际应用场景
// 电商订单金额计算 $subtotal = Math::add('99.99', '49.99'); // 149.98(商品小计) $discount = Math::discount($subtotal, '0.9'); // 134.98(打9折) $tax = Math::tax($discount, '0.13'); // 17.55(13%税) $total = Math::add($discount, $tax, 2); // 152.53(总金额) // 贷款利息计算 $principal = 100000; // 本金10万 $rate = 0.05; // 年利率5% $years = 5; // 5年 $simpleInterest = Math::simpleInterest($principal, $rate, $years); // 25000(简单利息) $compoundInterest = Math::compoundInterest($principal, $rate, $years); // 27628.16(复利) // 数据分析 $scores = [85, 92, 78, 90, 88, 95, 82]; $avg = Math::average($scores, 2); // 87.14(平均分) $median = Math::median($scores, 2); // 88.00(中位数) $stdDev = Math::standardDeviation($scores, 2); // 5.67(标准差) // 价格范围检查 $price = 99.99; $minPrice = 50; $maxPrice = 100; if (Math::inRange($price, $minPrice, $maxPrice)) { echo '价格在合理范围内'; } // 数值格式化显示 $amount = 1234567.89123; $formatted = Math::format($amount, 2, true); // 1,234,567.89
地理位置处理模块
特性
- ✅ 使用Haversine公式计算距离
- ✅ 支持多种距离单位(公里、英里、米)
- ✅ 支持方位角和中点计算
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
distance() |
计算两点距离 | Geo::distance(39.9042, 116.4074, 31.2304, 121.4737) |
isValid() |
验证坐标 | Geo::isValid(39.9042, 116.4074) |
bearing() |
计算方位角 | Geo::bearing(39.9042, 116.4074, 31.2304, 121.4737) |
midpoint() |
计算中点坐标 | Geo::midpoint(39.9042, 116.4074, 31.2304, 121.4737) |
使用示例
distance() - 计算两个坐标之间的距离
$distance = Geo::distance(39.9042, 116.4074, 31.2304, 121.4737, 'km'); // 北京到上海的距离,约1067公里
isValid() - 坐标验证
$valid = Geo::isValidCoordinate(39.9042, 116.4074); // true
midpoint() - 计算两个坐标之间的中点
$midpoint = Geo::midpoint(39.9042, 116.4074, 31.2304, 121.4737); // [35.593729492520936,119.07801023725797]
bearing() - 计算两个坐标之间的方位角
$bearing = Geo::bearing(39.9042, 116.4074, 31.2304, 121.4737); // 153.0726752205012
toRadians() - 将角度转换为弧度
$bearing = Geo::toRadians(90); // 1.5707963267948966
toDegrees() - 将弧度转换为角度
$bearing = Geo::toDegrees(1.5707963267948966); // 90
toDMS() - 将十进制度数转换为度分秒(DMS)
$bearing = Geo::toDMS(90, true); // [90,0,0,"N"] $bearing = Geo::toDMS(90, false); // [90,0,0,"E"]
toDecimal() - 将度分秒(DMS)转换为十进制度数
$bearing = Geo::toDecimal(90,0,0,"N"); // 90
gcj02ToBd09() - GCJ02坐标转BD09坐标
$bd01 = Geo::gcj02ToBd09(39.9042, 116.4074); // [39.9105, 116.4138]
bd09ToGcj02() - BD09坐标转GCJ02坐标
$gcj01 = Geo::bd09ToGcj02(39.9105, 116.4138); // [39.9042, 116.4074]
wgs84ToGcj02() - WGS84坐标转GCJ02坐标(中国火星坐标系)
$gcj02 = Geo::wgs84ToGcj02(39.9042, 116.4074); // [39.9056, 116.4136]
gcj02ToWgs84() - GCJ02坐标转WGS84坐标
$wgs01 = Geo::gcj02ToWgs84(39.9056, 116.4136); // [39.9042, 116.4074]
wgs84ToBd09() - WGS84坐标转BD09坐标
$bd02 = Geo::wgs84ToBd09(39.9042, 116.4074); // [39.9119, 116.4200]
bd09ToWgs84() - BD09坐标转WGS84坐标
$wgs02 = Geo::bd09ToWgs84(39.9119, 116.4200); // [39.9042, 116.4074]
IP地址处理模块
特性
- ✅ 获取IP地址版本
- ✅ 验证IP地址格式
- ✅ 获取IP地址位置信息
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
getLocation() |
获取IP地址位置信息 | Ip::getLocation(192.168.10.88) |
isValid() |
验证IP地址格式 | Ip::isValid(192.168.10.88) |
isPrivate() |
检查IP地址是否为私有/内部地址 | Ip::isPrivate(192.168.10.88) |
getVersion() |
获取IP地址版本 | Ip::getVersion(192.168.10.88) |
使用示例
getRealIp() - 获取真实客户端IP地址
$ip = Ip::getRealIp();
isValid() - 验证IP地址格式是否有效
$valid = Ip::isValid('192.168.1.1'); // true
isPrivate() - 检查IP地址是否为私有/内部地址
$private = Ip::isPrivate('192.168.1.1'); // true
getVersion() - 获取IP地址版本
$ip = Ip::isPrivate();
toLong() - 将IP地址转换为长整数
$long = Ip::toLong('192.168.1.1'); // 3232235777
fromLong() - 将长整数转换为IP地址
$ipStr = Ip::toString(3232235777); // '192.168.1.1'
getLocation() - 获取IP地址位置信息
$local = Ip::getLocation('192.168.1.1');
isFromCountry() - 检查IP地址是否来自特定国家
$ip = Ip::isFromCountry('192.168.1.1', 'CN');
getType() - 获取IP地址类型
$type = Ip::getType('192.168.1.1');
代码生成模块
特性
- ✅ UUID生成
- ✅ 订单号生成(时间戳+随机数)
- ✅ 邀请码生成(自定义长度和字符集)
- ✅ URL安全码生成(Base64URL编码)
- ✅ 注册码生成(支持分段显示)
- ✅ 线程安全的随机数生成(random_int)
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
uuid() |
生成UUID | Crypto::uuid() |
orderNo() |
生成订单号 | Crypto::orderNo('') |
inviteCode() |
生成邀请码 | Crypto::inviteCode(6, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') |
urlSafeCode() |
生成URL安全码 | Crypto::urlSafeCode(16) |
registerCode() |
生成注册码 | Crypto::registerCode(16, 4, '-') |
使用示例
uuid() - 生成uuid
$uuid = Generate::uuid();
orderNo() - 生成订单号
// 生成不带前缀的订单号 $orderNo = Generate::orderNo(); // 202512251200001234 // 生成带前缀的订单号 $orderNo = Generate::orderNo('ORD'); // ORD202507261234561234
inviteCode() - 生成邀请码
$inviteCode = Generate::inviteCode(8); // 8位邀请码
urlSafeCode() - 生成URL安全码
$urlSafe = Generate::urlSafeCode(16); // URL安全的随机码
registerCode() - 生成注册码
$registerCode = Generate::registerCode(12, 4); // 12位注册码,每4位分隔
加解密处理模块
特性
- ✅ MD5加密(支持加盐)
- ✅ 密码哈希(基于PHP原生password_hash)
- ✅ SSL对称加密(AES-256-GCM)
- ✅ HMAC签名(支持多种算法)
- ✅ 双模式调用:实例调用 + 静态调用
- ✅ 符合安全最佳实践
- ✅ PHP8.3+只读属性确保配置安全
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
md5() |
MD5加密(支持加盐) | Crypto::md5('123456', 'salt') |
passwordHash() |
密码哈希 | Crypto::passwordHash('123456') |
passwordVerify() |
密码验证 | Crypto::passwordVerify('123456', $hash) |
sslEncrypt() |
SSL对称加密 | Crypto::sslEncrypt('data', $key) |
sslDecrypt() |
SSL对称解密 | Crypto::sslDecrypt($encrypted, $key) |
使用示例
Crypto - 加密引擎说明
use Cdyun\PhpTool\Crypto; // Sodium引擎 - 推荐使用(性能更高,安全性更强) // 需要PHP扩展:sodium $crypto = new Crypto('your_key', Crypto::ENGINE_SODIUM); // OpenSSL引擎 - 通用选择 // 需要PHP扩展:openssl $crypto = new Crypto('your_key', Crypto::ENGINE_OPENSSL); // 自动选择引擎 - 自动选择最优引擎 $crypto = new Crypto('your_key', Crypto::ENGINE_AUTO);
Crypto - 加密模式说明
use Cdyun\PhpTool\Crypto; // 标准模式 - Base64编码 $crypto = new Crypto('your_key', Crypto::ENGINE_AUTO, Crypto::MODE_STANDARD); $encrypted = $crypto->encrypt('敏感数据'); // 输出示例: VEhJUz1mYWxzZVZlcnNpb249MS4wJmtleT1zZWN1cmVfazEyMw== // URL安全模式 - Base64URL编码(无=号,适合URL传输) $crypto = new Crypto('your_key', Crypto::ENGINE_AUTO, Crypto::MODE_URL_SAFE); $encrypted = $crypto->encrypt('敏感数据'); // 输出示例: VEhJUz1mYWxzZVZlcnNpb249MS4wJmtleT1zZWN1cmVfazEyMw // 紧凑模式 - 十六进制编码(最短长度,适合存储) $crypto = new Crypto('your_key', Crypto::ENGINE_AUTO, Crypto::MODE_COMPACT); $encrypted = $crypto->encrypt('敏感数据'); // 输出示例: 54484349533b66756c73652076657273696f6e20312e302e303b6b65793d7365637572655f6b313233
Crypto - 基础加解密
use Cdyun\PhpTool\Crypto; // 创建加密实例 $crypto = new Crypto('your_secret_key_2025'); // 加密数据 $encrypted = $crypto->encrypt('这是需要加密的敏感数据'); echo $encrypted; // 输出示例: VEhJUz1mYWxzZVZlcnNpb249MS4wJmtleT1zZWN1cmVfazEyMw== // 解密数据 $decrypted = $crypto->decrypt($encrypted); echo $decrypted; // 输出: 这是需要加密的敏感数据 // 使用不同的密钥 $crypto2 = new Crypto('another_key'); $encrypted2 = $crypto2->encrypt('另一个敏感数据'); // 解密(需要使用相同的密钥) $decrypted2 = $crypto2->decrypt($encrypted2);
Crypto - 静态方法调用
use Cdyun\PhpTool\Crypto; // 静态加密(使用默认密钥) $encrypted = Crypto::encrypt('敏感数据'); $decrypted = Crypto::decrypt($encrypted); // 使用自定义密钥的静态方法 $encrypted = (new Crypto('custom_key'))->encrypt('敏感数据'); $decrypted = (new Crypto('custom_key'))->decrypt($encrypted);
md5() - MD5加密(支持加盐)
// 基础MD5加密 $md5 = Crypto::md5('123456'); // e10adc3949ba59abbe56e057f20f883e // 加盐MD5加密 $salt = 'your_custom_salt'; $md5WithSalt = Crypto::md5('123456', $salt); // 52c69e3a57331081823331c4e6999d23 // 多次加盐(提高安全性) $doubleSalt = Crypto::md5(Crypto::md5('123456'), $salt);
passwordHash() - 密码哈希
$password = 'my_secure_password'; $hash = Crypto::passwordHash($password);
passwordVerify() - 密码哈希验证
// 密码正确 $isValid = Crypto::passwordVerify('my_secure_password', $hash); // 密码错误 $isValid = Crypto::passwordVerify('wrong_password', $hash); // 密码哈希更新(重新哈希) if (password_needs_rehash($hash, PASSWORD_DEFAULT)) { $newHash = Crypto::passwordHash($password); }
Crypto - HMAC签名
use Cdyun\PhpTool\Crypto\Crypto; // SHA256签名(默认) $signature = Crypto::hmac('待签名数据', 'your_secret_key'); echo $signature; // 输出示例: 3a6eb0790f39ac87c94f3856b2dd2c5d110e0f9b0e9c9d6e7b8c9d0e1f2a3b4c // SHA512签名 $signature512 = Crypto::hmac('待签名数据', 'your_secret_key', 'sha512'); echo $signature512; // 输出示例: a4e6b8c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2 // MD5签名 $signatureMd5 = Crypto::hmac('待签名数据', 'your_secret_key', 'md5'); echo $signatureMd5; // 输出示例: 1a1dc06f7a0b2c8d9e0f1a2b3c4d5e6f7 // 数据完整性验证 $originalData = '订单数据'; $originalSignature = Crypto::hmac($originalData, 'api_secret'); $receivedData = '订单数据'; $receivedSignature = $_SERVER['HTTP_SIGNATURE'] ?? ''; if (hash_equals($originalSignature, $receivedSignature)) { echo '数据完整性验证通过'; } else { echo '数据可能被篡改'; }
Crypto - SSL对称加解密
use Cdyun\PhpTool\Crypto; // 使用自定义密钥进行SSL加密 $key = 'your_ssl_key_32_bytes_long!'; $crypto = new Crypto(); $encrypted = $crypto->sslEncrypt('SSL加密数据', $key); echo $encrypted; // 输出示例: VGhpc0lzU1NMY0VuY3J5cHRlZERhdGE= $decrypted = $crypto->sslDecrypt($encrypted, $key); echo $decrypted; // 输出: SSL加密数据
Crypto - 错误处理
use Cdyun\PhpTool\Crypto; $crypto = new Crypto('your_key'); try { $encrypted = $crypto->encrypt('敏感数据'); $decrypted = $crypto->decrypt($encrypted); echo '加解密成功: ' . $decrypted; } catch (\Exception $e) { echo '加解密失败: ' . $e->getMessage(); } // 密钥错误时的解密异常 try { $wrongCrypto = new Crypto('wrong_key'); $decrypted = $wrongCrypto->decrypt($encrypted); } catch (\Exception $e) { echo '解密失败(密钥错误): ' . $e->getMessage(); }
使用场景
use Cdyun\PhpTool\Crypto; // 场景1:用户敏感信息加密存储 function saveUserSensitiveData(Crypto $crypto, array $data): array { return [ 'id' => $data['id'], 'name' => $data['name'], 'encrypted_phone' => $crypto->encrypt($data['phone']), 'encrypted_id_card' => $crypto->encrypt($data['id_card']) ]; } function getUserSensitiveData(Crypto $crypto, array $userData): array { return [ 'id' => $userData['id'], 'name' => $userData['name'], 'phone' => $crypto->decrypt($userData['encrypted_phone']), 'id_card' => $crypto->decrypt($userData['encrypted_id_card']) ]; } // 场景2:API请求签名验证 function verifyApiRequest(Crypto $crypto, array $params, string $signature): bool { $expectedSignature = Crypto::hmac(json_encode($params), $apiSecret); return hash_equals($expectedSignature, $signature); } // 场景3:密码安全存储 function registerUser(string $password): string { return Crypto::passwordHash($password); } function verifyUserPassword(string $password, string $hash): bool { return Crypto::passwordVerify($password, $hash); }
HTTP请求处理模块
特性
- ✅ 支持HTTP/HTTPS协议
- ✅ 支持请求超时设置
- ✅ 支持请求头设置
- ✅ 支持请求参数设置
- ✅ 支持响应状态码、响应头、响应体获取
- ✅ 支持异常处理
- ✅ 支持多种HTTP方法(GET/POST/PUT/PATCH/DELETE/HEAD/OPTIONS)
- ✅ 支持多种内容类型(JSON/form/multipart)
- ✅ 灵活的请求选项配置
- ✅ 响应处理和错误处理
核心方法
| 方法名 | 功能描述 | 调用示例 |
|---|---|---|
get() |
GET请求 | Curl::get('https://api.example.com', $data) |
post() |
POST请求 | Curl::post('https://api.example.com', $data) |
put() |
PUT请求 | Curl::put('https://api.example.com/1', $data) |
delete() |
DELETE请求 | Curl::delete('https://api.example.com/1') |
head() |
HEAD请求 | Curl::head('https://api.example.com') |
options() |
OPTIONS请求 | Curl::options('https://api.example.com') |
使用示例
setDefaultOptions() - 设置默认配置
Curl::setDefaultOptions([ 'timeout' => 60, 'connect_timeout' => 20, ])
getDefaultOptions() - 获取默认配置
Curl::getDefaultOptions() // 返回默认配置 // [ // 'timeout' => 60, // 'connect_timeout' => 20, // 'verify' => true, // ]
reset() - 重置客户端实例
Curl::reset()
options() - OPTIONS请求
$response = Curl::options('https://api.example.com', [], []);
head() - HEAD请求
$response = Curl::head('https://api.example.com', [], [], []);
get() - GET请求
$response = Curl::get('https://api.example.com', [], [], []); $response = Curl::get('https://api.example.com/users', ['page' => 1, 'limit' => 10]);
post() - POST请求
$response = Curl::post('https://api.example.com', [], [], []); $response = Curl::post('https://api.example.com', ['name' => 'John', 'email' => 'john@example.com']);
postForm() - POST表单请求
$response = Curl::postForm('https://api.example.com', [], [], []); $response = Curl::postForm('https://api.example.com', ['name' => 'John', 'email' => 'john@example.com']);
postMultipart() - POST文件上传请求
$response = Curl::postMultipart( 'https://api.example.com/upload', [ ['name' => 'file', 'contents' => fopen('/path/to/file.jpg', 'r')] ] );
put() - PUT请求
$response = Curl::put('https://api.example.com/users/1', [], [], []); $response = Curl::put('https://api.example.com/users/1', ['name' => 'Updated Name']);
putForm() - PUT表单请求
$response = Curl::put('https://api.example.com/users/1', [], [], []); $response = Curl::put('https://api.example.com/users/1', ['name' => 'Updated Name']);
delete() - PUT请求
$response = Curl::delete('https://api.example.com/users/1', [], [], []); $response = Curl::delete('https://api.example.com/users/1');
patch() - PATCH请求
$response = Curl::patch('https://api.example.com/users/1', [], [], []); $response = Curl::patch('https://api.example.com/users/1', [ 'status' => 'active' ]);
版本要求
- PHP >= 8.1
- ext-openssl
- ext-json
许可证
MIT License