PHP code example of cdyun / php-tool
1. Go to this page and download the library: Download cdyun/php-tool library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cdyun / php-tool example snippets
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' => []
// ]
// ]
$flat = Arr::list($tree, 'children');
$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 = 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']
// ]
$arr = [
['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]
];
$path = Arr::getParentIds($arr, 4, 'parent_id');
// 输出:
// ['1','2']
$arr = [
['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]
];
// 不含自身
$path = Arr::getChildIds($arr, 1, 'parent_id',false);
// 输出:
// ['2','3','4']
// 含自身
$path = Arr::getChildIds($arr, 1, 'parent_id',true);
// 输出:
// ['1','2','3','4']
use Cdyun\PhpTool\Arr;
$arr1 = [
'user' => ['name' => '张三', 'age' => 25],
'settings' => ['theme' => 'dark']
];
$arr2 = [
'user' => ['age' => 26, 'email' => '[email protected] '],
'settings' => ['language' => 'zh-CN']
];
$merged = Arr::deepMerge($arr1, $arr2);
// 输出:
// [
// 'user' => ['name' => '张三', 'age' => 26,'email' => '[email protected] '],
// 'settings' => ['theme' => 'dark', 'language' => 'zh-CN']
// ]
$value = Arr::get($arr1, 'user.name'); // '张三'
$value = Arr::get($arr1, ['user', 'age']); // 25
$value = Arr::get($arr1, 'user.email', '[email protected] '); // '[email protected] '
$result = Arr::set($arr1, 'user.age', 26);
$exists = Arr::has($arr1, 'user.name'); // true
$exists = Arr::has($arr1, 'user.email'); // false
$only = Arr::only($arr1, ['user.name', 'user.age']);
// 输出: ['user' => ['name' => '张三', 'age' => 25]]
$except = Arr::except($array1, ['settings']);
// 输出: ['user' => ['name' => '张三', 'age' => 25]]
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 = Arr::count($flatArray, 'parent_id');
// 输出: [0 => 1, 1 => 2, 2 => 1]
$sum = Arr::sum($flatArray, 'id'); // 10
$avg = Arr::avg($flatArray, 'id'); // 2.5
$min = Arr::max($flatArray, 'id'); // 4
$min = Arr::min($flatArray, 'id'); // 1
use Cdyun\PhpTool\Arr;
// PHP 8.4+使用原生array_first
$first = Arr::first([1, 2, 3, 4, 5]); // 1
$first = Arr::first([]); // null
// PHP 8.4+使用原生array_last
$last = Arr::last([1, 2, 3, 4, 5]); // 5
$last = Arr::last([]); // null
// 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
// 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
// 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
// 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
$mapped = Arr::map([1, 2, 3, 4, 5], fn($n) => $n * 2); // [2, 4, 6, 8, 10]
$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](过滤空值)
$sum = Arr::reduce([1, 2, 3, 4, 5], fn($carry, $n) => $carry + $n, 0); // 15
$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
$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'
$hasSome = Arr::some([1, 2, 3, 4, 5], fn($n) => $n > 3); // true
$allMatch = Arr::every([1, 2, 3, 4, 5], fn($n) => $n > 0); // true
$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 = Arr::containsKey(['a' => 1, 'b' => 2], 'a'); // true
$isEmpty = Arr::isEmpty([]); // true
$isEmpty = Arr::isEmpty([1, 2, 3]); // false
$isAssoc = Arr::isAssoc(['a' => 1, 'b' => 2]); // true
$isAssoc = Arr::isAssoc([1, 2, 3]); // false
$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]
];
// 升序
$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]
// ]
// 先按age升序,age相同时按id降序
$multiSorted = Arr::multiSort($users, ['age', 'id'], ['asc', 'desc']);
$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](保留键名)
$shuffled = Arr::shuffle([1, 2, 3, 4, 5]); // 随机打乱顺序
$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](非严格比较)
$users = [
['id' => 1, 'name' => '张三'],
['id' => 2, 'name' => '李四'],
['id' => 1, 'name' => '张三']
];
$unique = Arr::multiUnique($users, 'id');
// 输出:
// [
// ['id' => 1, 'name' => '张三'],
// ['id' => 2, 'name' => '李四']
// ]
$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]
$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](保留键名)
$chunked = Arr::chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3);
// 输出: [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
$merged = Arr::merge([1, 2, 3], [4, 5, 6]); // [1, 2, 3, 4, 5, 6]
$merged = Arr::mergeRecursive(['a' => 1], ['b' => 2]); // ['a' => 1, 'b' => 2]
$diff = Arr::diff([1, 2, 3, 4, 5], [2, 4]); // [1, 3, 5]
$diff = Arr::diffKey(['a' => 1, 'b' => 2, 'c' => 3], ['b' => 2]); // ['a' => 1, 'c' => 3]
$intersect = Arr::intersect([1, 2, 3, 4, 5], [2, 4, 6]); // [2, 4]
$intersect = Arr::intersectKey(['a' => 1, 'b' => 2, 'c' => 3], ['b' => 2, 'c' => 4]); // ['b' => 2, 'c' => 3]
$json = Arr::toJson(['name' => '张三', 'age' => 25]); // '{"name":"张三","age":25}'
$array = Arr::fromJson('{"name":"张三","age":25}'); // ['name' => '张三', 'age' => 25]
$flattened = Arr::flatten([1, [2, [3, [4, 5]]]]); // [1, 2, 3, 4, 5]
$keys = Arr::keys(['a' => 1, 'b' => 2, 'c' => 3]); // ['a', 'b', 'c']
$values = Arr::values(['a' => 1, 'b' => 2, 'c' => 3]); // [1, 2, 3]
$flipped = Arr::flip(['a' => 1, 'b' => 2, 'c' => 3]); // [1 => 'a', 2 => 'b', 3 => 'c']
$users = [
['id' => 1, 'name' => '张三', 'age' => 25],
['id' => 2, 'name' => '李四', 'age' => 30]
];
$names = Arr::column($users, 'name'); // ['张三', '李四']
$indexed = Arr::column($users, 'name', 'id'); // [1 => '张三', 2 => '李四']
$mapped = Arr::mapKeys(['a' => 1, 'b' => 2], fn($key) => strtoupper($key)); // ['A' => 1, 'B' => 2]
$mapped = Arr::mapValues(['a' => 1, 'b' => 2], fn($value) => $value * 2); // ['a' => 2, 'b' => 4]
$combined = Arr::combine(['a', 'b', 'c'], [1, 2, 3]); // ['a' => 1, 'b' => 2, 'c' => 3]
$filled = Arr::fillKeys(['a', 'b', 'c'], 0); // ['a' => 0, 'b' => 0, 'c' => 0]
$filled = Arr::fill(0, 5, 0); // [0, 0, 0, 0, 0]
$random = Arr::random([1, 2, 3, 4, 5]); // 随机返回其中一个元素
$randomMany = Arr::randomMany([1, 2, 3, 4, 5], 3); // 随机返回3个元素
$array = [1, 2, 3, 4, 5];
$first = Arr::shift($array); // 1,$array变为[2, 3, 4, 5]
$last = Arr::pop($array); // 5,$array变为[2, 3, 4]
$count = Arr::unshift($array, 0); // 4,$array变为[0, 2, 3, 4]
$count = Arr::push($array, 5); // 5,$array变为[0, 2, 3, 4, 5]
$removed = Arr::remove([1, 2, 3, 2, 4, 2], 2); // [1, 3, 4]
$removed = Arr::removeKey(['a' => 1, 'b' => 2, 'c' => 3], 'b'); // ['a' => 1, 'c' => 3]
$masked = Str::mask('13800138000', 3, 4, '*'); // '138****8000'
$masked = Str::mask('[email protected] ', 2, 4, '*'); // 'us****@example.com'
$phone = Str::maskPhone('13800138000'); // '138****8000'
$phone = Str::maskPhone('13800138000', 3, 4); // '138****8000'
$email = Str::maskEmail('[email protected] '); // 'us***@example.com'
$email = Str::maskEmail('[email protected] ', 2, 3); // 'us***@example.com'
$idCard = Str::maskIdCard('110101199001011234'); // '110101********1234'
$idCard = Str::maskIdCard('110101199001011234', 6, 8); // '110101********1234'
$bankCard = Str::maskBankCard('6222021234567890123'); // '622202********0123'
$bankCard = Str::maskBankCard('6222021234567890123', 6, 10); // '622202********0123'
$name = Str::maskName('张三'); // '张*'
$name = Str::maskName('欧阳修'); // '欧阳*'
$name = Str::maskName('张三丰', 1, 1); // '张*丰'
$length = Str::length('你好世界'); // 4(使用mb_strlen)
$truncated = Str::truncate('这是一段很长的文本内容', 10); // '这是一段很长的文...'
$truncated = Str::truncate('这是一段很长的文本内容', 10, '---'); // '这是一段很长的文---'
$limited = Str::limit('这是一段很长的文本内容', 10); // '这是一段很长的文...'
$limited = Str::limit('短文本', 10); // '短文本'(不超过长度不截断)
$wordTruncate = Str::wordTruncate('This is a long text content', 10); // 'This is a...'
$snake = Str::snake('helloWorld'); // 'hello_world'
$snake = Str::snake('HelloWorld'); // 'hello_world'
$snake = Str::snake('HelloWorld', '-'); // 'hello-world'
$snake = Str::toSnake('helloWorld'); // 'hello_world'
$snake = Str::toSnake('HelloWorld'); // 'hello_world'
$snake = Str::toSnake(['AaBbCc'=>1]); // ['aa_bb_cc'=>1]
$camel = Str::camel('hello_world'); // 'helloWorld'
$camel = Str::camel('hello-world'); // 'helloWorld'
$camel = Str::camel('hello_world', '-'); // 'helloWorld'
$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'
$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 = Str::ucfirst('hello'); // 'Hello'
$lcfirst = Str::lcfirst('Hello'); // 'hello'
$ucwords = Str::ucwords('hello world'); // 'Hello World'
$upper = Str::upper('hello'); // 'HELLO'
$lower = Str::lower('HELLO'); // 'hello'
$swap = Str::swap('Hello'); // 'hELLO'
$title = Str::title('hello world'); // 'Hello World'
$contains = Str::contains('hello world', 'world'); // true
$contains = Str::contains('hello world', 'php'); // false
$startsWith = Str::startsWith('hello world', 'hello'); // true
$startsWith = Str::startsWith('hello world', 'world'); // false
$endsWith = Str::endsWith('hello world', 'world'); // true
$endsWith = Str::endsWith('hello world', 'hello'); // false
$pos = Str::pos('hello world', 'world'); // 6
$pos = Str::pos('hello world', 'php'); // false
$pos = Str::rpos('hello world world', 'world'); // 12
$pos = Str::rpos('hello world', 'php'); // false
composer
├── src/
│ ├── Arr.php // 数组处理主类
│ ├── Str.php // 字符串处理主类
│ ├── Time.php // 时间处理主类
│ ├── Math.php // 数学计算主类
│ ├── Geo.php // 地理位置主类
│ ├── Ip.php // IP地址处理主类
│ ├── Crypto.php // 加解密主类(命名简洁)
│ ├── Generate.php // 代码生成工具类
│ ├── Curl.php // HTTP请求主类
│ ├── Dir.php // 目录文件处理
│ ├── helpers.php // 全局辅助函数文件
│ ├──...... // 其他│
│
├── composer.json // Composer配置
├── README.md // 使用文档
php
$count = Str::count('hello world world', 'world'); // 2
php
$match = Str::match('hello123', '/^[a-z]+\d+$/'); // true
$match = Str::match('hello', '/^[a-z]+\d+$/'); // false
php
$replaced = Str::replace('hello world', 'world', 'php'); // 'hello php'
php
$replaced = Str::replaceArray('hello world', ['hello' => 'hi', 'world' => 'php']); // 'hi php'
php
$replaced = Str::replaceRegex('hello123world', '/\d+/', '456'); // 'hello456world'
php
$replaced = Str::substrReplace('hello world', 'php', 6, 5); // 'hello php'
php
$parts = Str::split('hello,world,php', ','); // ['hello', 'world', 'php']
php
$array = Str::toArray('hello,world,php', ','); // ['hello', 'world', 'php']
php
$string = Str::fromArray(['hello', 'world', 'php'], ','); // 'hello,world,php'
php
$joined = Str::join(['hello', 'world', 'php'], ','); // 'hello,world,php'
php
$concat = Str::concat('hello', ' ', 'world'); // 'hello world'
php
$trimmed = Str::trim(' hello world '); // 'hello world'
php
$ltrim = Str::ltrim(' hello world '); // 'hello world '
php
$rtrim = Str::rtrim(' hello world '); // ' hello world'
php
$clean = Str::clean(' hello world '); // 'helloworld'
php
$padded = Str::padLeft('123', 6, '0'); // '000123'
php
$padded = Str::padRight('123', 6, '0'); // '123000'
php
$padded = Str::padBoth('123', 7, '*'); // '**123**'
php
$repeated = Str::repeat('hello', 3); // 'hellohellohello'
php
$reversed = Str::reverse('hello'); // 'olleh'
php
$random = Str::random(16); // 16位随机字符串
php
$numeric = Str::numeric(6); // 6位随机数字字符串
php
$alpha = Str::alpha(8); // 8位随机字母字符串
php
$base64 = Str::toBase64('hello'); // 'aGVsbG8='
php
$decoded = Str::fromBase64('aGVsbG8='); // 'hello'
php
$urlEncoded = Str::toUrlEncode('hello world'); // 'hello%20world'
php
$urlDecoded = Str::fromUrlEncode('hello%20world'); // 'hello world'
php
$htmlEncoded = Str::toHtmlEntities('<div>hello</div>'); // '<div>hello</div>'
php
$htmlDecoded = Str::fromHtmlEntities('<div>hello</div>'); // '<div>hello</div>'
php
$xml = Str::toXml(['name' => '张三', 'age' => 25], 'root'); // '<root><name>张三</name><age>25</age></root>'
php
$decoded = Str::fromXml('<root><name>张三</name><age>25</age></root>'); // ['name' => '张三', 'age' => 25]
php
$binary = Str::toBinary('hello'); // '0110100001100101011011000110110001101111'
php
$decoded = Str::fromBinary('0110100001100101011011000110110001101111'); // 'hello'
php
$hex = Str::toHex('hello'); // '68656c6c6f'
php
$decoded = Str::fromHex('68656c6c6f'); // 'hello'
php
$md5 = Str::md5('hello'); // '5d41402abc4b2a76b9719d911017c592'
php
$sha1 = Str::sha1('hello'); // 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
php
$sha256 = Str::sha256('hello'); // '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
php
$sha512 = Str::sha512('hello'); // '9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043'
php
$isEmail = Str::isEmail('[email protected] '); // true
$isEmail = Str::isEmail('invalid-email'); // false
php
$isUrl = Str::isUrl('https://example.com'); // true
$isUrl = Str::isUrl('not-a-url'); // false
php
$isIp = Str::isIp('192.168.1.1'); // true
$isIp = Str::isIp('not-an-ip'); // false
php
$isIpv4 = Str::isIpv4('192.168.1.1'); // true
$isIpv4 = Str::isIpv4('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // false
php
$isIpv6 = Str::isIpv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // true
$isIpv6 = Str::isIpv6('192.168.1.1'); // false
php
$isPhone = Str::isPhone('13800138000'); // true
$isPhone = Str::isPhone('12345678901'); // false
php
$isIdCard = Str::isIdCard('110101199001011234'); // true
$isIdCard = Str::isIdCard('123456789012345678'); // false
php
$isBankCard = Str::isBankCard('6222021234567890123'); // true
$isBankCard = Str::isBankCard('1234567890'); // false
php
$isNumeric = Str::isNumeric('12345'); // true
$isNumeric = Str::isNumeric('abc123'); // false
php
$isAlpha = Str::isAlpha('hello'); // true
$isAlpha = Str::isAlpha('hello123'); // false
php
$isAlnum = Str::isAlnum('hello123'); // true
$isAlnum = Str::isAlnum('hello world'); // false
php
$isHex = Str::isHex('1a2b3c'); // true
$isHex = Str::isHex('1g2h3i'); // false
php
$isBinary = Str::isBinary('01010101'); // true
$isBinary = Str::isBinary('12345678'); // false
php
$isJson = Str::isJson('{"name":"张三"}'); // true
$isJson = Str::isJson('not json'); // false
php
$isXml = Str::isXml('<root>hello</root>'); // true
$isXml = Str::isXml('not xml'); // false
php
$isBase64 = Str::isBase64('aGVsbG8='); // true
$isBase64 = Str::isBase64('not base64'); // false
php
$array = Str::toArray('hello,world,php', ','); // ['hello', 'world', 'php']
php
$string = Str::fromArray(['hello', 'world', 'php'], ','); // 'hello,world,php'
php
$query = Str::toQuery(['name' => '张三', 'age' => 25]); // 'name=%E5%BC%A0%E4%B8%89&age=25'
php
$array = Str::fromQuery('name=%E5%BC%A0%E4%B8%89&age=25'); // ['name' => '张三', 'age' => 25]
php
$indented = Str::indent('hello', 4); // ' hello'
php
$unindented = Str::unindent(' hello'); // 'hello'
php
$first = Str::first('hello'); // 'h'
php
$last = Str::last('hello'); // 'o'
php
$firstN = Str::firstN('hello', 2); // 'he'
php
$lastN = Str::lastN('hello', 2); // 'lo'
php
$removed = Str::removeFirst('hello'); // 'ello'
php
$removed = Str::removeLast('hello'); // 'hell'
php
$removed = Str::removeFirstN('hello', 2); // 'llo'
php
$removed = Str::removeLastN('hello', 2); // 'hel'
php
$count = Str::count('hello world world', 'world'); // 2
php
$wordCount = Str::wordCount('hello world php'); // 3
php
$charCount = Str::charCount('hello'); // 5
php
$byteLength = Str::byteLength('你好'); // 6(UTF-8编码)
php
$escaped = Str::escapeHtml('<div>hello</div>'); // '<div>hello</div>'
php
$escaped = Str::escapeSql("O'Reilly"); // "O\'Reilly"
php
$escaped = Str::escapeRegex('hello.world'); // 'hello\.world'
php
$compare = Str::compare('hello', 'hello'); // 0(相等)
$compare = Str::compare('hello', 'world'); // -15(不相等)
php
$similarity = Str::similarity('hello', 'hello'); // 1.0(完全相同)
$similarity = Str::similarity('hello', 'world'); // 0.2(相似度)
php
$distance = Str::distance('hello', 'hello'); // 0(相同)
$distance = Str::distance('hello', 'world'); // 4(编辑距离)
php
$timestamp = time();
php
$newTime = Time::add($timestamp, 3600); // 加1小时
$newTime = Time::add($timestamp, 86400); // 加1天
$newTime = Time::add($timestamp, 604800); // 加1周
php
$diff = Time::diff(time(), time() - 3600); // 3600(秒)
$diff = Time::diff(time() - 86400, time()); // 86400(秒)
php
$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小时前'