|
1 | 1 | # hejunjie/utils
|
2 | 2 |
|
3 |
| -一个零碎但实用的 PHP 工具函数集合库。 |
| 3 | +<div align="center"> |
| 4 | + <a href="./README.md">English</a>|<a href="./README.zh-CN.md">简体中文</a> |
| 5 | + <hr width="50%"/> |
| 6 | +</div> |
4 | 7 |
|
5 |
| -> 🌱 很多实现原理都不复杂,但总在项目里反复写,写多了心态有点崩。 |
6 |
| -> 干脆集中起来,不重复造轮子,省心省力。 |
| 8 | +A lightweight and practical PHP utility library that offers a collection of commonly used helper functions for files, strings, arrays, and HTTP requests—designed to streamline development and support everyday PHP projects. |
7 | 9 |
|
8 |
| -## 安装方式 |
| 10 | +> 🌱 Many of these implementations are conceptually simple, but rewriting them repeatedly across projects can become quite tedious. |
| 11 | +> To make things easier, I gathered these utilities in one place to avoid reinventing the wheel — saving both time and effort. |
| 12 | +
|
| 13 | +## Installation |
9 | 14 |
|
10 | 15 | ```bash
|
11 | 16 | composer require hejunjie/utils
|
12 | 17 | ```
|
13 | 18 |
|
14 |
| -## 用途 & 初衷 |
15 |
| - |
16 |
| -这是一个典型的「开发者懒癌工具库」。 |
17 |
| - |
18 |
| -在日常项目中,你可能也经常遇到这些情况: |
19 |
| - |
20 |
| -一个数组操作写了无数次; |
21 |
| - |
22 |
| -文件大小格式化总要搜 StackOverflow; |
23 |
| - |
24 |
| -想判断字符串是不是 JSON、是不是手机号、是不是邮箱,结果各种复制粘贴; |
25 |
| - |
26 |
| -项目变了,原来的工具类又得重新封装一遍... |
27 |
| - |
28 |
| -所以,我把这些经常用、常见又简单的小方法统一整理了一下,写成这个工具库。不求高级,不追花哨,目标就是:简单易用,解放双手。 |
29 |
| - |
30 |
| -## 当前支持的方法(更新中) |
31 |
| - |
32 |
| -## 当前支持的方法列表 |
33 |
| - |
34 |
| -### 字符串操作 |
35 |
| - |
36 |
| -| 方法 | 说明 | |
37 |
| -| :------------------------------- | :------------------------------- | |
38 |
| -| Str::containsAny() | 检查字符串中是否存在数组中的内容 | |
39 |
| -| Str::padString() | 补充特定字符串,使其达到指定长度 | |
40 |
| -| Str::replaceFirst() | 替换字符串中第一次出现的搜索值 | |
41 |
| -| Str::generateRandomString() | 生成随机字符串 | |
42 |
| -| Str::getRandomSurname() | 获取随机姓氏 | |
43 |
| -| Str::truncateString() | 截断字符串 | |
44 |
| -| Str::maskString() | 字符串掩码 | |
45 |
| -| Str::removeWhitespace() | 移除字符串中的所有空白字符 | |
46 |
| -| Str::stringEncrypt() | 字符串加密(AES-128-CBC) | |
47 |
| -| Str::stringDecrypt() | 字符串解密(AES-128-CBC) | |
48 |
| -| Str::formatDurationFromSeconds() | 根据秒数转换为可读性时间 | |
49 |
| - |
50 |
| -### 数组操作 |
51 |
| - |
52 |
| -| 方法 | 说明 | |
53 |
| -| :----------------------------- | :------------------------------- | |
54 |
| -| Arr::arrayIntersect() | 获取两个数组的交集 | |
55 |
| -| Arr::sortByField() | 根据二维数组中的指定字段排序 | |
56 |
| -| Arr::removeDuplicatesByField() | 根据二维数组中指定字段去重 | |
57 |
| -| Arr::groupByField() | 根据二维数组中的指定字段进行分组 | |
58 |
| -| Arr::csvToArray() | 读取 CSV 文件并返回数组格式 | |
59 |
| -| Arr::arrayToCsv() | 数组转换为 CSV 格式的字符串 | |
60 |
| -| Arr::xmlParse() | xml 解析为数组 | |
61 |
| -| Arr::arrayToXml() | 数组转换为 xml | |
62 |
| - |
63 |
| -### 文件操作 |
64 |
| - |
65 |
| -| 方法 | 说明 | |
66 |
| -| :--------------------------------------- | :-------------------------- | |
67 |
| -| FileUtils::readFile() | 读取文件内容 | |
68 |
| -| FileUtils::writeToFile() | 将内容写入文件 | |
69 |
| -| FileUtils::getFileExtension() | 获取文件扩展名 | |
70 |
| -| FileUtils::joinPaths() | 拼接多个路径 | |
71 |
| -| FileUtils::getFileNameWithoutExtension() | 获取文件名(不带扩展名) | |
72 |
| -| FileUtils::fileDelete() | 删除文件或目录 | |
73 |
| -| FileUtils::writeUniqueLinesToFile() | 获取文件中的唯一行(去重) | |
74 |
| -| FileUtils::getCommonLinesFromFiles() | 从多个文件中获取交集行 | |
75 |
| -| FileUtils::extractColumnFromCsvFiles() | 从多个 csv 文件中快速提取列 | |
76 |
| - |
77 |
| -### 网络请求操作 |
78 |
| - |
79 |
| -| 方法 | 说明 | |
80 |
| -| :---------------------------- | :----------------------- | |
81 |
| -| HttpClient::sendGetRequest() | 使用 cURL 发送 GET 请求 | |
82 |
| -| HttpClient::sendPostRequest() | 使用 cURL 发送 POST 请求 | |
83 |
| - |
84 |
| -### 图片操作 |
85 |
| - |
86 |
| -| 方法 | 说明 | |
87 |
| -| :-------------------------- | :----------------------------------------------------- | |
88 |
| -| Img::downloadImageFromUrl() | 从 URL 下载图片 | |
89 |
| -| Img::imageToBase64() | 将图片转换为 Base64 字符串 | |
90 |
| -| Img::base64ToImage() | 将 Base64 字符串保存为图片 | |
91 |
| -| Img::compressImage() | 压缩图片到指定大小(单位 KB),支持多种格式转换为 JPEG | |
92 |
| -| Img::resizeImage() | 调整图片分辨率,保持宽高比 | |
93 |
| - |
94 |
| -### 导出操作 |
95 |
| - |
96 |
| -| 方法 | 说明 | |
97 |
| -| :----------------------------- | :------------ | |
98 |
| -| DataExporter::exportTxt() | 导出 TXT | |
99 |
| -| DataExporter::exportMarkdown() | 导出 Markdown | |
100 |
| -| DataExporter::exportCsv() | 导出 CSV | |
101 |
| -| DataExporter::exportJson() | 导出 JSON | |
102 |
| -| DataExporter::exportSql() | 导出 SQL | |
103 |
| -| DataExporter::exportHtml() | 导出 HTML | |
104 |
| -| DataExporter::exportXml() | 导出 XML | |
105 |
| - |
106 |
| -## 🔧 更多工具包(可独立使用,也可统一安装) |
107 |
| - |
108 |
| -本项目最初是从 [hejunjie/tools](https://github.com/zxc7563598/php-tools) 拆分而来,如果你想一次性安装所有功能组件,也可以使用统一包: |
| 19 | +## Purpose & Motivation |
| 20 | + |
| 21 | +This is a typical "Developer's Time-Saving Toolkit." |
| 22 | +In everyday projects, you may often find yourself dealing with situations like: |
| 23 | + |
| 24 | +Rewriting array operations over and over again; |
| 25 | + |
| 26 | +- Searching StackOverflow every time you need to format file sizes; |
| 27 | +- Copy-pasting checks to determine if a string is JSON, a phone number, or an email address; |
| 28 | +- Having to refactor your utility classes whenever the project changes... |
| 29 | + |
| 30 | +So, I’ve compiled these simple and frequently-used methods into this toolkit. It’s not about complexity or fancy features; the goal is to keep it simple, easy to use, and save you time and effort. |
| 31 | + |
| 32 | +## Currently Supported Methods (Updating) |
| 33 | + |
| 34 | +## List of Currently Supported Methods |
| 35 | + |
| 36 | +### String Operations |
| 37 | + |
| 38 | +| method | describe | |
| 39 | +| :------------------------------- | :----------------------------------------------------------- | |
| 40 | +| Str::containsAny() | Check if a string contains any of the elements in an array | |
| 41 | +| Str::padString() | Pad a string to a specified length with a specific character | |
| 42 | +| Str::replaceFirst() | Replace the first occurrence of a search value in a string | |
| 43 | +| Str::generateRandomString() | Generate a random string | |
| 44 | +| Str::getRandomSurname() | Get a random surname | |
| 45 | +| Str::truncateString() | Truncate a string | |
| 46 | +| Str::maskString() | String masking | |
| 47 | +| Str::removeWhitespace() | Remove all whitespace characters from a string | |
| 48 | +| Str::stringEncrypt() | Encrypt a string (AES-128-CBC) | |
| 49 | +| Str::stringDecrypt() | Decrypt a string (AES-128-CBC) | |
| 50 | +| Str::formatDurationFromSeconds() | Convert seconds to a human-readable time format | |
| 51 | + |
| 52 | +### Array Operations |
| 53 | + |
| 54 | +| method | describe | |
| 55 | +| :----------------------------- | :---------------------------------------------------------- | |
| 56 | +| Arr::arrayIntersect() | Get the intersection of two arrays | |
| 57 | +| Arr::sortByField() | Sort a 2D array by a specific field | |
| 58 | +| Arr::removeDuplicatesByField() | Remove duplicates from a 2D array based on a specific field | |
| 59 | +| Arr::groupByField() | Group a 2D array by a specific field | |
| 60 | +| Arr::csvToArray() | Read a CSV file and return it as an array | |
| 61 | +| Arr::arrayToCsv() | Convert an array to a CSV formatted string | |
| 62 | +| Arr::xmlParse() | Parse XML into an array | |
| 63 | +| Arr::arrayToXml() | Convert an array to XML | |
| 64 | + |
| 65 | +### File Operations |
| 66 | + |
| 67 | +| method | describe | |
| 68 | +| :--------------------------------------- | :---------------------------------------------- | |
| 69 | +| FileUtils::readFile() | Read file contents | |
| 70 | +| FileUtils::writeToFile() | Write content to a file | |
| 71 | +| FileUtils::getFileExtension() | Get the file extension | |
| 72 | +| FileUtils::joinPaths() | Join multiple paths together | |
| 73 | +| FileUtils::getFileNameWithoutExtension() | Get the file name (without extension) | |
| 74 | +| FileUtils::fileDelete() | Delete a file or directory | |
| 75 | +| FileUtils::writeUniqueLinesToFile() | Get unique lines from a file (deduplication) | |
| 76 | +| FileUtils::getCommonLinesFromFiles() | Get intersecting lines from multiple files | |
| 77 | +| FileUtils::extractColumnFromCsvFiles() | Quickly extract columns from multiple CSV files | |
| 78 | + |
| 79 | +### Network Request Operations |
| 80 | + |
| 81 | +| method | describe | |
| 82 | +| :---------------------------- | :----------------------------- | |
| 83 | +| HttpClient::sendGetRequest() | Send a GET request using cURL | |
| 84 | +| HttpClient::sendPostRequest() | Send a POST request using cURL | |
| 85 | + |
| 86 | +### Image Operations |
| 87 | + |
| 88 | +| method | describe | |
| 89 | +| :-------------------------- | :------------------------------------------------------------------------------------------------- | |
| 90 | +| Img::downloadImageFromUrl() | Download an image from a URL | |
| 91 | +| Img::imageToBase64() | Convert an image to a Base64 string | |
| 92 | +| Img::base64ToImage() | Save a Base64 string as an image | |
| 93 | +| Img::compressImage() | Compress an image to a specified size (in KB), with support for converting various formats to JPEG | |
| 94 | +| Img::resizeImage() | Resize an image while maintaining aspect ratio | |
| 95 | + |
| 96 | +### Export Operations |
| 97 | + |
| 98 | +| method | describe | |
| 99 | +| :----------------------------- | :-------------- | |
| 100 | +| DataExporter::exportTxt() | Export TXT | |
| 101 | +| DataExporter::exportMarkdown() | Export Markdown | |
| 102 | +| DataExporter::exportCsv() | Export CSV | |
| 103 | +| DataExporter::exportJson() | Export JSON | |
| 104 | +| DataExporter::exportSql() | Export SQL | |
| 105 | +| DataExporter::exportHtml() | Export HTML | |
| 106 | +| DataExporter::exportXml() | Export XML | |
| 107 | + |
| 108 | +## 🔧 Additional Toolkits (Can be used independently or installed together) |
| 109 | + |
| 110 | +This project was originally extracted from [hejunjie/tools](https://github.com/zxc7563598/php-tools). |
| 111 | +To install all features in one go, feel free to use the all-in-one package: |
109 | 112 |
|
110 | 113 | ```bash
|
111 | 114 | composer require hejunjie/tools
|
112 | 115 | ```
|
113 | 116 |
|
114 |
| -当然你也可以按需选择安装以下功能模块: |
| 117 | +Alternatively, feel free to install only the modules you need: |
| 118 | + |
| 119 | +[hejunjie/utils](https://github.com/zxc7563598/php-utils) - A lightweight and practical PHP utility library that offers a collection of commonly used helper functions for files, strings, arrays, and HTTP requests—designed to streamline development and support everyday PHP projects. |
115 | 120 |
|
116 |
| -[hejunjie/cache](https://github.com/zxc7563598/php-cache) - 多层缓存系统,基于装饰器模式。 |
| 121 | +[hejunjie/cache](https://github.com/zxc7563598/php-cache) - A layered caching system built with the decorator pattern. Supports combining memory, file, local, and remote caches to improve hit rates and simplify cache logic. |
117 | 122 |
|
118 |
| -[hejunjie/china-division](https://github.com/zxc7563598/php-china-division) - 中国省市区划分数据包。 |
| 123 | +[hejunjie/china-division](https://github.com/zxc7563598/php-china-division) - Regularly updated dataset of China's administrative divisions with ID-card address parsing. Distributed via Composer and versioned for use in forms, validation, and address-related features |
119 | 124 |
|
120 |
| -[hejunjie/error-log](https://github.com/zxc7563598/php-error-log) - 责任链日志上报系统。 |
| 125 | +[hejunjie/error-log](https://github.com/zxc7563598/php-error-log) - An error logging component using the Chain of Responsibility pattern. Supports multiple output channels like local files, remote APIs, and console logs—ideal for flexible and scalable logging strategies. |
121 | 126 |
|
122 |
| -[hejunjie/mobile-locator](https://github.com/zxc7563598/php-mobile-locator) - 国内手机号归属地 & 运营商识别。 |
| 127 | +[hejunjie/mobile-locator](https://github.com/zxc7563598/php-mobile-locator) - A mobile number lookup library based on Chinese carrier rules. Identifies carriers and regions, suitable for registration checks, user profiling, and data archiving. |
123 | 128 |
|
124 |
| -[hejunjie/address-parser](https://github.com/zxc7563598/php-address-parser) - 收货地址智能解析工具,支持从非结构化文本中提取用户/地址信息。 |
| 129 | +[hejunjie/address-parser](https://github.com/zxc7563598/php-address-parser) - An intelligent address parser that extracts name, phone number, ID number, region, and detailed address from unstructured text—perfect for e-commerce, logistics, and CRM systems. |
125 | 130 |
|
126 |
| -[hejunjie/url-signer](https://github.com/zxc7563598/php-url-signer) - URL 签名工具,支持对 URL 进行签名和验证。 |
| 131 | +[hejunjie/url-signer](https://github.com/zxc7563598/php-url-signer) - A PHP library for generating URLs with encryption and signature protection—useful for secure resource access and tamper-proof links. |
127 | 132 |
|
128 |
| -[hejunjie/google-authenticator](https://github.com/zxc7563598/php-google-authenticator) - Google Authenticator 及类似应用的密钥生成、二维码创建和 OTP 验证。 |
| 133 | +[hejunjie/google-authenticator](https://github.com/zxc7563598/php-google-authenticator) - A PHP library for generating and verifying Time-Based One-Time Passwords (TOTP). Compatible with Google Authenticator and similar apps, with features like secret generation, QR code creation, and OTP verification. |
129 | 134 |
|
130 |
| -[hejunjie/simple-rule-engine](https://github.com/zxc7563598/php-simple-rule-engine) - 一个轻量、易用的 PHP 规则引擎,支持多条件组合、动态规则执行。 |
| 135 | +[hejunjie/simple-rule-engine](https://github.com/zxc7563598/php-simple-rule-engine) - A lightweight and flexible PHP rule engine supporting complex conditions and dynamic rule execution—ideal for business logic evaluation and data validation. |
131 | 136 |
|
132 |
| -👀 所有包都遵循「轻量实用、解放双手」的原则,能单独用,也能组合用,自由度高,欢迎 star 🌟 或提 issue。 |
| 137 | +👀 All packages follow the principles of being lightweight and practical — designed to save you time and effort. They can be used individually or combined flexibly. Feel free to ⭐ star the project or open an issue anytime! |
133 | 138 |
|
134 | 139 | ---
|
135 | 140 |
|
136 |
| -该库后续将持续更新,添加更多实用功能。欢迎大家提供建议和反馈,我会根据大家的意见实现新的功能,共同提升开发效率。 |
| 141 | +This library will continue to be updated with more practical features. Suggestions and feedback are always welcome — I’ll prioritize new functionality based on community input to help improve development efficiency together. |
0 commit comments