一组有用的PHP实用工具

v1.7.2 2023-05-01 18:29 UTC

This package is auto-updated.

Last update: 2024-09-26 15:17:10 UTC


README

PHP的基本实用函数

ArrayUtils

toStdClass(array $array): stdClass

将数组转换为stdClass

$users = [
    "value" => [
        "users" => [
            [
                "username" => "user",
                "password" => "********",
                "email" => "user@example.com"
            ]
        ]
    ],
    "token" => "********************************"
];

$users = ArrayUtils::toStdClass($users);

var_dump($users);
/* output
object(stdClass)#2 (2) {
    ["value"]=>
    object(stdClass)#4 (1) {
        ["users"]=>
        object(stdClass)#5 (1) {
        ["0"]=>
        object(stdClass)#6 (3) {
            ["username"]=>
            string(4) "user"
            ["password"]=>
            string(8) "********"
            ["email"]=>
            string(16) "user@example.com"
        }
        }
    }
    ["token"]=>
    string(32) "********************************"
}
*/
randomize(array $array): array

随机化给定的数组

$characters = str_split("Command_String");
$randomized_characters = ArrayUtils::randomize($characters);

foreach ($randomized_characters as $character) {
    echo $character;
}

StringUtils

getBetween(string $start, string $end, string $string, bool $include_start_end_with_response = false): string

在字符串中获取指定点之间的文本,并返回它。

$greeting = "My name is Bob! What's yours?";

$name = StringUtils::getBetween("is ", "!", $greeting);

echo $name; // output: Bob

如果您想返回开始和结束位置,可以将第四个参数设置为true,这种情况的一个很好的用例是从文本块中解析JSON字符串

$text = 'fdsjhyasdfuygfdsuygfduygfsd{"name": "Bob"}asduygasduyauysduytasduy?';

$json = StringUtils::getBetween("{", "}", $text, true);

echo $json; // output: {"name": "Bob"}

var_dump(json_decode($json)); 
/* output: 
object(stdClass)#3 (1) {
  ["name"]=>
  string(5) "Bob"
}
*/

GeneratorUtils

uuid(int $length = 16, array $characters = []): string

生成UUID。

通用方法将返回一个16位字母数字字符串

echo GeneratorUtils::uuid(); // output: 6MwqCff0wdpUl1sdw

您可以根据需要调整长度

echo GeneratorUtils::uuid(5); // output: 8zWgWw

您还可以为生成器提供要使用的字符

echo GeneratorUtils::uuid(10, [1, 0]); // output: 11110100100

FileSystemUtils

getAllFiles(string $directory, bool $recursive = false): array

获取目录中的所有文件,如果第二个参数为true,则包括子目录中的文件在内的返回数组

getAllSubDirectories(string $directory, bool $recursive = false): array

获取目录中的所有子目录,如果递归为true,则包括子目录的子目录

getAllFilesWithExtensions(string $directory, array $extensionsToFind, bool $recursive = false): array

获取目录中的所有文件,这些文件具有提供的扩展名之一。如果第三个参数为true,则也会搜索目录的子目录。

ColorUtils

RGBAtoHEX(int $red, int $blue, int $green, ?int $alpha = null): string

将RGBA颜色代码转换为HEX颜色代码

HEXtoRGBA(string $hex): array

将HEX颜色代码转换为RGBA颜色代码。

FileSizeUtils

convertFileSize(FileSizeUtils $from_type, FileSizeUtils $to_type, float $from_size): float

将文件大小从一种类型转换为另一种类型

humanReadable(FileSizeUtils $type, float $size, int $decimals = 0): string

创建并减小格式,该格式将类型缩写附加到末尾。

echo FileSizeUtils::humanReadable(FileSizeUtils::MEGABYTE, 5000); // output: 5 GB
reduceFileSize(FileSizeUtils $type, float $size): stdClass

将文件大小减小到最小值,在小于1之前。然后返回一个具有类型属性的对象std和大小属性的新大小。

var_dump(FileSizeUtils::humanReadable(FileSizeUtils::MEGABYTE, 5000));
/**
 *  object(stdClass)#12 (2) {
 *  ["type"]=>
 *      enum(CommandString\Utils\FileSizeUtils::GIGABYTE)
 *  ["size"]=>
 *      float(5)
 *  }
 */