echron/tools

PHP 工具库

1.7.0 2024-07-29 20:24 UTC

README

Unit Tests Latest Stable Version

关于

Echron PHP 工具库提供了一套便捷的类和方法,用于简化 PHP 开发。

安装

使用以下命令安装最新版本

composer require echron/tools

用法

将秒数输出为可读字符串

echo \Echron\Tools\Time::readableSeconds(60 * 24);
>> 24 minutes
echo \Echron\Tools\Time::readableSeconds(60 + 3.4221580028534);
>> 1 minute, 3.42 seconds
echo \Echron\Tools\Time::readableSeconds(3.455669555);
>> 3.46 seconds

检查数组中是否有重复项

$a = \Echron\Tools\ArrayHelper::hasDuplicates(['red', 'green', 'purple']);
print_r($a);
>> false

$b = \Echron\Tools\ArrayHelper::hasDuplicates(['red', 'green', 'purple','red]);
print_r($b);
>> true

通过唯一值过滤数组

$a = \Echron\Tools\ArrayHelper::unique(['red', 'green', 'purple'],['orange','red']);
print_r($a);
>> ['red','green','purple','orange']

限制字符串长度而不切断单词

$a = \Echron\Tools\StringHelper::limitWords('This is a basic string', 20);
print_r($a);
>> "This is a basic"

可以传递一个结束标记,当字符串被截断时将添加到字符串末尾。结束标记包含在最大字符字符串中,即使添加了结束标记,结果也不会超过最大字符数。

$result = \Echron\Tools\StringHelper::limitWords('This is a basic string', 20, ' ...');
print_r($result);
>> "This is a basic ..."

限制字符串长度

$result = \Echron\Tools\StringHelper::limit('This is a basic string', 20);
print_r($result);
>> "This is a basic stri"

添加结束标记

$result = \Echron\Tools\StringHelper::limit('This is a basic string', 20, ' ...');
print_r($result);
>> "This is a basic  ...', $result)"

CSV

  • 将 CSV 文件读取到数组中
$result = \Echron\Tools\CsvHelper::parseCSVFile('file.csv');
  • 根据数组写入 CSV 文件
$result = \Echron\Tools\CsvHelper::toCSVFile(['field'=> 'value'], 'file.csv');