ideaworks/php-utils
Composer 安装命令:
composer require ideaworks/php-utils
包简介
A collection of String, Date and Array manipulations and checks.
关键字:
README 文档
README
A collection of String, Date and Array manipulations and checks.
Requirements
- PHP >= 5.3
Installation
Install the package through Composer. Edit your project's composer.json file by adding:
"require": { "lftbrts/php-utils": "dev-master" }
Next, run the Composer update command from the terminal:
composer update --no-dev
If you want to run the phpunit tests, run Composer from the terminal:
composer update
Usage
- Arr A collection of methods to manipulate and check arrays.
- Dt A collection of methods for calculating and checking dates.
- Str A collection of methods to manipulate and check strings.
###Arr
A collection of methods to manipulate and check arrays.
Arr::array_is_assoc($array)
Determine if an array is associative.
if (true == Arr::array_is_assoc( array( 'foo' => 'baz', 'lemonade' => array('juice' => 'lemon') ))) { echo 'the array is an associative array.'; }
Arr::array_remove_key($array, ...)
Remove key(s) from an array.
$arr = array( 'foo' => 'baz', 'pink', 'panther' ); $res = Arr::array_remove_key($arr, 'foo'); // $res contains indexes 0 and 1 $res = Arr::array_remove_key($arr, 1, 0, 'foo'); // $res is empty $res = Arr::array_remove_key($arr, 'baz'); // $res == $arr
Arr::array_change_key($array, $oldkey, $newkey)
Change a key of an associative array.
$arr = array('foo', 'baz'); $arr = Arr::array_change_key($arr, 'foo', 'Foo'); // $arr contains key 'Foo'
Arr::array_contain_keys(array $needle, array $haystack)
Check if an associative array contains certain keys.
Searches haystack for needle.
Arr::array_remove_value($array, ...)
Remove value(s) from an array.
$arr = array( 'foo' => 'baz', 'pink', 'panther' ); $res = ArrayUtils::array_remove_value($arr, 'panther'); // $res contains 'foo' and 'pink' $res = Arr::array_remove_value($arr, 'baz'); // $res contains 'pink' and 'panther' $res = Arr::array_remove_value($arr, 'foo', 'panther'); // $res contains only 'pink' $res = Arr::array_remove_value($arr, 'lion'); // $res == $arr
Arr::array_remove_empty_value($array)
Remove all empty values from an array.
$arr = array( 'a' => null, 'b' => '', 'c' => " ", 'pinkpanther' ); $res = Arr::array_remove_empty_value($arr); // $res contains 'pinkpanther' and 'c'
Arr::array_walkup($needle, array $haystack)
Search for the structure by a given value on a multidimensional array.
This will break execution on the first occurrence of the matched value.
$arr = array( 'foo' => 'baz', 'pink' => array( 'panther', array( 'juice' => 'lemon' ) ) ); if (false !== ($structure = Arr::array_walkup('lemon', $arr))) { /* * $structure = array(3) { * [0]=> string(4) "pink" * [1]=> int(1) * [2]=> string(4) "juice" * } */ // output below is $arr['pink'][1]['juice']; echo 'lemon has been found in $arr[' . implode('][', $structure) .']'; }
Arr::array_reverse_sets($array, integer)
Reverses the order of an array group-wise.
$arr = array(1,2,3,4,5,6,7,8,9,10,11,12); // cut the $arr into 3 groups in reversed order $res = Arr::array_reverse_sets($arr, 3); // $res = [10,11,12,7,8,9,4,5,6,1,2,3]
###Dt
A collection of methods for calculating and checking dates.
Dt::getDateRangeDays(DateTime, DateTime)
Retrieve the days between two dates.
$start = \DateTime::createFromFormat('Y-m-d', '2015-02-01'); $end = \DateTime::createFromFormat('Y-m-d', '2015-03-01'); $days = Dt::getDateRangeDays($start, $end); echo $days; // output 28
Dt::checkInDateRange(DateTime, DateTime, DateTime)
Check if a date is within two dates (total days).
$start = \DateTime::createFromFormat('Y/m/d', '2015/04/12'); $end = clone $start; $check = clone $start; $end->modify('+1 hour'); $check->modify('+10 minutes'); if (true == Dt::checkInDateRange($start, $end, $check)) { echo $check->format('Y-m-d') . ' is between ' . $start->format('Y-m-d') . ' and ' . $end->format('Y-m-d'); }
Dt::intersectDateRanges(DateTime, DateTime, DateTime, DateTime)
Determine if two date-ranges intersects. If so, the total days between the dates will be returned.
$start1 = \DateTime::createFromFormat('Y-m-d', '2015-02-01'); $end1 = \DateTime::createFromFormat('Y-m-d', '2015-03-25'); $start2 = \DateTime::createFromFormat('Y-m-d', '2015-03-20'); $end2 = \DateTime::createFromFormat('Y-m-d', '2015-06-01'); if (false !== ($days = Dt::intersectDateRanges($start1, $end1, $start2, $end2))) { echo 'the dates intersect with ' . $days . ' days'; // $days == 6 }
Dt::addMonths(DateTime, $months)
Makes sure that adding months always ends up in the month you would expect.
Works for positive and negative values.
$date = \DateTime::createFromFormat('Y-m-d', '2012-02-29'); $enddate = Dt::addMonths($date, 36); // add three years echo $enddate->format('Y-m-d'); // outputs 2015-02-28, because 2015 isn't a leap year
Dt::addYears(DateTime, $years)
Makes sure that adding years always ends up in the month you would expect.
Works for positive and negative values.
$date = \DateTime::createFromFormat('Y-m-d', '2012-02-29'); $enddate = Dt::addYears($date, 3); echo $enddate->format('Y-m-d'); // outputs 2015-02-28
###Str
A collection of methods to manipulate and check strings.
Str::areEqual($param1, $param2, $casesensitive = true)
Determine if two strings are equal.
if (false == Str::areEqual('pinkpanther', 'PiNKpanther')) { echo 'the strings are not equal.'; } // ignore case sensitivity if (true == Str::areEqual('pinkpanther', 'PiNKpanther', false)) { echo 'the strings are equal.'; }
Str::areEqualReverse($param1, $param2, $casesensitive = true)
Determine if a string is equal to a reversed string.
if (true == Str::areEqualReverse('password', 'drowssap')) { echo 'the reversed string is equal to the first string.'; } // ignore case sensitivity if (true == Str::areEqualReverse('PASSword', 'drowssap', false)) { echo 'the reversed string is equal to the first string.'; } if (false == Str::areEqualReverse('password', 'password')) { echo 'the reversed string is not equal to the first string.'; }
Str::maxLength($str, $length)
Check whether a string is smaller or equal a given length.
if (false == Str::maxLength('pinkpanther', 5)) { echo 'the string is larger than five chars.'; } if (true == StringUtils::maxLength('paul', 4)) { echo 'the string is smaller or equal four chars.'; }
Str::minLength($str, $length)
Check whether a string is larger or equal a given length.
if (false == Str::minLength('pinkpanther', 5)) { echo 'the string is larger than five chars.'; } if (true == StringUtils::minLength('paul', 4)) { echo 'the string is larger or equal four chars.'; } if (false == Str::minLength('paul', 5)) { echo 'the string is smaller than five chars.'; }
Str::isEmpty($str)
Determine if a string is empty or contains only white spaces.
if (true == Str::isEmpty(" ") || true == Str::isEmpty(null) || true == Str::isEmpty('')) { echo 'The string is empty or contains only white spaces.'; }
##License
The MIT License (MIT)
Copyright (c) 2015 Ulf Tiburtius
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ideaworks/php-utils 适用场景与选型建议
ideaworks/php-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「compare string」 「compare date」 「manipulate array」 「compare array」 「calculate date」 「date ranges intersect」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ideaworks/php-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ideaworks/php-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ideaworks/php-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Tools to convert between .NET and PHP date formats
PHP Semantic Versioning Parser and Comparator
Encode integer IDs using a preset or customized symbol set
base62 encoder and decoder also for big numbers with Laravel integration
Small class for checking ip addresses
统计信息
- 总下载量: 75
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-04-13