buroq/php-helper

PHP 辅助函数库,易于使用

0.1.0 2023-12-18 07:22 UTC

This package is auto-updated.

Last update: 2024-09-03 15:34:52 UTC


README

Build Status

关于

buroq/php-helper 是一个包含 45 个有用 PHP 辅助函数的集合(PHP 5.6, 7.*)。
使用 composer 安装后,全局函数可以在代码的任何地方访问

安装

composer require buroq/php-helper

示例

<?php

dump( 'any content' );

如果您的项目中已经定义了相同名称的函数(内置和用户定义),则该函数将不会在环境中注册。因此,不会出现与现有函数的 冲突

然而,每个函数都可以通过适当的 use-语句以静态方式访问

示例

<?php

use BUROQ\Helper\Util as util;

util::dump( 'any content' );

致谢

这个库使用了以下著名且出色的库

测试

所有函数都经过单元测试和 PHP 版本的测试。Build Status

安装

使用 composer 安装最新的 buroq/php-helper

composer require buroq/php-helper

同时确保需要您的 composer 自动加载文件

require __DIR__ . '/vendor/autoload.php';

安装后,新的全局 PHP 函数可以在代码的任何地方使用。要访问辅助类中的(几乎相同)静态函数,请向文件中添加正确的 use 语句

示例

<?php

use BUROQ\Helper\Dev as dev;

if( dev::isIphone() ) {
   // Do something here
}

可用的 PHP 函数

目录

API 文档

目录

Arr

辅助类,提供对有用的 PHP 数组函数的简单访问。

类 Arr

  • 全名:\BUROQ\Helper\Arr

isAssoc

检测给定的值是否是关联数组。

Arr::isAssoc( array $array ): boolean

is_assoc

相关全局函数(描述见上文)。

( 返回 )

is_assoc( array $array ): boolean

示例

$array = [
    'foo' => 'bar'
];

is_assoc( $array );

// bool(true)
  • 此方法为 静态参数:

返回值

如果数组是关联的,则为真,否则为假。

toObject

将数组转换为对象。

Arr::toObject( array $array ): object|null

to_object

相关全局函数(描述见上文)。

( 返回 )

to_object( array $array ): object|null

示例

$array = [
    'foo' => [
         'bar' => 'baz'
    ]
];

$obj = to_object($array);
echo $obj->foo->bar;

// baz
  • 此方法为 静态参数:

返回值

转换后的数组的std对象表示。

dump

将字符串或对象转换为数组。

Arr::dump( string|object $var ): array|null

to_array

相关全局函数(描述见上文)。

( 返回 )

to_array( string|object $var ): array|null

示例 1(字符串)

$var = 'php';
to_array( $var );

// (
//     [0] => p
//     [1] => h
//     [2] => p
// )

示例 2(对象)

$var = new stdClass;
$var->foo = 'bar';

to_array( $var );

// (
//     [foo] => bar
// )
  • 此方法为 静态参数:

返回值

转换后的字符串或对象的数组表示。错误时返回null。

first

返回数组的第一个元素。

Arr::first( array $array ): mixed

array_first

相关全局函数(描述见上文)。

( 返回 )

array_first( array $array ): mixed

示例

$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

array_first( $array )

// bar
  • 此方法为 静态参数:

返回值

第一个元素的值,不带键。混合类型。

last

返回数组的最后一个元素。

Arr::last( array $array ): mixed

array_last

相关全局函数(描述见上文)。

( 返回 )

array_last( array $array ): mixed

示例

$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

array_last( $array )

// qux
  • 此方法为 静态参数:

返回值

最后一个元素的值,不带键。混合类型。

get

通过点表示法根据键在数组中获取值。

Arr::get( string $key, array $array ): mixed

array_get

相关全局函数(描述见上文)。

( 返回 )

array_get( string key, array $array ): mixed

示例

$array = [
     'foo' => 'bar',
     'baz' => [
         'qux => 'foobar'
     ]
];

array_get( 'baz.qux', $array );

// foobar
  • 此方法为 静态参数:

返回值

搜索到的值,否则为null。

set

使用点表示法在数组中设置值。

Arr::set( string $key, mixed $value, array &$array ): boolean

array_set

相关全局函数(描述见上文)。

( 返回 )

array_set( string key, mixed value, array $array ): boolean

示例 1

$array = [
     'foo' => 'bar',
     'baz' => [
         'qux => 'foobar'
     ]
];

array_set( 'baz.qux', 'bazqux', $array );

// (
//     [foo] => bar
//     [baz] => [
//         [qux] => bazqux
//     ]
// )

示例 2

$array = [
     'foo' => 'bar',
     'baz' => [
         'qux => 'foobar'
     ]
];

array_set( 'baz.foo', 'bar', $array );

// (
//     [foo] => bar
//     [baz] => [
//         [qux] => bazqux
//         [foo] => bar
//     ]
// )
  • 此方法为 静态参数:

返回值

如果新值成功设置,则为真,否则为假。

Dev

辅助类,它通过与用户代理结合提供对有用的php函数的轻松访问。

类 Dev

  • 全名:\BUROQ\Helper\Dev

isSmartphone

确定当前设备是否为智能手机。

Dev::isSmartphone(  ): boolean

is_smartphone

相关全局函数(描述见上文)。

( 返回 )

is_smartphone(  ): boolean

示例

if ( is_smartphone() ) {
     // I am a smartphone
}
  • 此方法为静态

返回值

如果当前访客使用智能手机,则为真,否则为假。

isMobile

检测当前访客是否使用移动设备(智能手机/平板电脑/手持设备)。

Dev::isMobile(  ): boolean

is_mobile

相关全局函数(描述见上文)。

( 返回 )

is_mobile(  ): boolean

示例

if ( is_mobile() ) {
     // I am a mobile device (smartphone/tablet or handheld)
}
  • 此方法为静态

返回值

如果当前访客使用移动设备,则为真,否则为假。

mobileDetect

获取一个单例MobileDetect对象以调用它提供的每个方法。

Dev::mobileDetect(  ): \Detection\MobileDetect

公共访问,供此类外部使用。Mobile_Detect文档:https://github.com/serbanghita/Mobile-Detect

此方法没有相关的全局函数!

( 返回 )

示例

Dev::mobileDetect()->version('Android');

// 8.1
  • 此方法为静态

返回值

一个单例MobileDetect对象,用于调用它提供的每个方法。

isTablet

确定当前访客是否使用平板电脑设备。

Dev::isTablet(  ): boolean

is_tablet

相关全局函数(描述见上文)。

( 返回 )

is_tablet(  ): boolean

示例

if ( is_tablet() ) {
     // I am a tablet
}
  • 此方法为静态

返回值

如果当前访客使用平板电脑设备,则为真,否则为假。

isDesktop

确定当前访客是否使用台式计算机。

Dev::isDesktop(  ): boolean

is_desktop

相关全局函数(描述见上文)。

( 返回 )

is_desktop(  ): boolean

示例

if ( is_desktop() ) {
     // I am a desktop computer (Mac, Linux, Windows)
}
  • 此方法为静态

返回值

如果当前访客使用台式计算机,则为真,否则为假。

isRobot

确定当前访客是否为搜索引擎/机器人/爬虫/蜘蛛。

Dev::isRobot(  ): boolean

is_robot

相关全局函数(描述见上文)。

( 返回 )

is_robot(  ): boolean

示例

if ( is_robot() ) {
     // I am a robot (search engine, bot, crawler, spider)
}
  • 此方法为静态

返回值

如果当前访客是搜索引擎/机器人/爬虫/蜘蛛,则为真,否则为假。

crawlerDetect

获取一个单例CrawlerDetect对象以调用它提供的每个方法。

Dev::crawlerDetect(  ): \Jaybizzle\CrawlerDetect\CrawlerDetect

公共访问,供此类外部使用。Crawler-Detect文档:https://github.com/JayBizzle/Crawler-Detect

此方法没有相关的全局函数!

( 返回 )

示例

Dev::crawlerDetect()->getMatches();

// Output the name of the bot that matched (if any)
  • 此方法为静态

isAndroid

确定当前设备是否运行Android操作系统。

Dev::isAndroid(  ): boolean

is_android

相关全局函数(描述见上文)。

( 返回 )

is_android(  ): boolean

示例

if ( is_android() ) {
     // I am an Android based device
}
  • 此方法为静态

返回值

如果当前访客使用基于Android的设备,则为真,否则为假。

isIphone

确定当前设备是否为iPhone。

Dev::isIphone(  ): boolean

is_iphone

相关全局函数(描述见上文)。

( 返回 )

is_iphone(  ): boolean

示例

if ( is_iphone() ) {
     // I am an iPhone
}
  • 此方法为静态

返回值

如果当前访客使用iPhone,则为真,否则为假。

isSamsung

确定当前设备是否来自三星。

Dev::isSamsung(  ): boolean

is_samsung

相关全局函数(描述见上文)。

( 返回 )

is_samsung(  ): boolean

示例

if ( is_samsung() ) {
     // I am a device from Samsung
}
  • 此方法为静态

返回值

如果当前访客使用三星设备,则为真,否则为假。

isIOS

确定当前设备是否运行iOS操作系统。

Dev::isIOS(  ): boolean

is_ios

相关全局函数(描述见上文)。

( 返回 )

is_ios(  ): boolean

示例

if ( is_ios() ) {
     // I am an iOS based device
}
  • 此方法为静态

返回值

如果当前访客使用iOS设备,则为真,否则为假。

Str

辅助类,它提供了对有用的php字符串函数的轻松访问。

类 Str

  • 全名:\BUROQ\Helper\Str

insert

在定义的位置将一个或多个字符串插入到另一个字符串中。

Str::insert( array $keyValue, string $string ): string

str_insert

相关全局函数(描述见上文)。

( 返回 )

str_insert( array $keyValue, string $string ): string

示例

$keyValue = [
     ':color' => 'brown',
     ':animal' => 'dog'
]
$string = 'The quick :color fox jumps over the lazy :animal.';

str_insert( $keyValue, $string );

// The quick brown fox jumps over the lazy dog.
  • 此方法为 静态参数:

返回值

替换后的字符串。

between

返回在左元素和右元素之间的内容。

Str::between( string $left, string $right, string $string ): array

str_between

相关全局函数(描述见上文)。

( 返回 )

str_between( string $left, string $right, string $string ): array

示例

$string = '<tag>foo</tag>foobar<tag>bar</tag>'

str_between( '<tag>', '</tag>' $string );

// (
//     [0] => foo
//     [1] => bar
// )
  • 此方法为 静态参数:

返回值

包含所有搜索匹配项的结果数组。

after

返回给定值之后字符串的部分。

Str::after( string $search, string $string ): string

str_after

相关全局函数(描述见上文)。

( 返回 )

str_after( string $search, string $string ): string

示例

$string = 'The quick brown fox jumps over the lazy dog';

str_after( 'fox' $string );

// jumps over the lazy dog
  • 此方法为 静态参数:

返回值

搜索字符串之后找到的字符串。开头将移除空白。

before

获取给定值之前字符串的部分。

Str::before( string $search, string $string ): string

str_before

相关全局函数(描述见上文)。

( 返回 )

str_before( string $search, string $string ): string

示例

$string = 'The quick brown fox jumps over the lazy dog';

str_before( 'fox' $string );

// The quick brown
  • 此方法为 静态参数:

返回值

在搜索字符串之前找到的字符串。末尾将移除空白。

limitWords

限制字符串中的单词数。将$end的值放置在字符串末尾。

Str::limitWords( string $string, integer $limit = 10, string $end = '...' ): string

str_limit_words

相关全局函数(描述见上文)。

( 返回 )

str_limit_words( string $string, int $limit = 10, string $end = '...' ): string

示例

$string = 'The quick brown fox jumps over the lazy dog';

str_limit_words( $string, 3 );

// The quick brown...
  • 此方法为 静态参数:

返回值

带有$end在末尾的有限字符串。

limit

限制字符串中的字符数。将$end的值放置在字符串末尾。

Str::limit( string $string, integer $limit = 100, string $end = '...' ): string

str_limit

相关全局函数(描述见上文)。

( 返回 )

str_limit( string $string, int $limit = 100, string $end = '...' ): string

示例

$string = 'The quick brown fox jumps over the lazy dog';

str_limit( $string, 15 );

// The quick brown...
  • 此方法为 静态参数:

返回值

带有$end在末尾的有限字符串。

contains

检查字符串是否包含给定的元素

Str::contains( string|array $needle, string $haystack ): boolean

str_contains

相关全局函数(描述见上文)。

( 返回 )

str_contains( string|array $needle, string $haystack ): boolean

示例

$string = 'The quick brown fox jumps over the lazy dog';
$array = [
     'cat',
     'fox'
];

str_contains( $array, $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果找到 $needle,则返回 true,否则返回 false。

containsIgnoreCase

测试字符串是否包含指定的元素。忽略大小写。

Str::containsIgnoreCase( string|array $needle, string $haystack ): boolean

str_icontains

相关全局函数(描述见上文)。

( 返回 )

str_icontains( string|array $needle, string $haystack ): boolean

示例

$string = 'The quick brown fox jumps over the lazy dog';
$array = [
     'Cat',
     'Fox'
];

str_icontains( $array, $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果找到 $needle,则返回 true,否则返回 false。

startsWith

确定给定的字符串是否以给定的子串开头。

Str::startsWith( string|array $needle, string $haystack ): boolean

str_starts_with

相关全局函数(描述见上文)。

( 返回 )

str_starts_with( string|array $needle, string $haystack ): boolean

示例

$string = 'The quick brown fox jumps over the lazy dog';
$array = [
     'Cat',
     'The'
];

str_starts_with( $array, $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果找到 $needle,则返回 true,否则返回 false。

startsWithIgnoreCase

确定给定的字符串是否以给定的子串开头。忽略大小写。

Str::startsWithIgnoreCase( string|array $needle, string $haystack ): boolean

str_istarts_with

相关全局函数(描述见上文)。

( 返回 )

str_istarts_with( string|array $needle, string $haystack ): boolean

示例

$string = 'The quick brown fox jumps over the lazy dog';
$array = [
     'cat',
     'the'
];

str_istarts_with( $array, $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果找到 $needle,则返回 true,否则返回 false。

endsWith

确定给定的字符串是否以给定的子串结尾。

Str::endsWith( string|array $needle, string $haystack ): boolean

str_ends_with

相关全局函数(描述见上文)。

( 返回 )

str_ends_with( string|array $needle, string $haystack ): boolean

示例

$string = 'The quick brown fox jumps over the lazy dog';
$array = [
     'cat',
     'dog'
];

str_ends_with( $array, $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果找到 $needle,则返回 true,否则返回 false。

endsWithIgnoreCase

确定给定的字符串是否以给定的子串结尾。

Str::endsWithIgnoreCase( string|array $needle, string $haystack ): boolean

str_iends_with

相关全局函数(描述见上文)。

( 返回 )

str_iends_with( string|array $needle, string $haystack ): boolean

示例

$string = 'The quick brown fox jumps over the lazy dog';
$array = [
     'Cat',
     'Dog'
];

str_iends_with( $array, $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果找到 $needle,则返回 true,否则返回 false。

afterLast

返回字符串中最后一个给定搜索值之后的部分。

Str::afterLast( string $search, string $string ): string

str_after_last

相关全局函数(描述见上文)。

( 返回 )

str_after_last( string $search, string $string ): string

示例

$path = "/var/www/html/public/img/image.jpg";

str_after_last( '/' $path );

// image.jpg
  • 此方法为 静态参数:

返回值

搜索字符串最后一个出现后的找到的字符串。开头将删除空格。

Util

辅助类,提供对常用 PHP 函数的轻松访问。

类 Util

  • 全名:\BUROQ\Helper\Util

isEmail

验证给定的电子邮件地址。

Util::isEmail( string $email ): boolean

is_email

相关全局函数(描述见上文)。

( 返回 )

is_email( string $email ): boolean

示例

$email = 'foobar@example.com';

is_email( $email );

// bool(true)
  • 此方法为 静态参数:

返回值

如果给定字符串是有效的电子邮件地址,则返回 true,否则返回 false。

ip

获取用户的当前 IP 地址。

Util::ip(  ): string|null

user_ip

相关全局函数(描述见上文)。

( 返回 )

ip(  ): null|string

示例

echo ip();

// 127.0.0.1
  • 此方法为静态

返回值

检测到的 IP 地址,如果未检测到 IP,则为 null。

cryptPassword

从给定的密码创建一个安全的散列。使用 CRYPT_BLOWFISH 算法。

Util::cryptPassword( string $password ): string

注意:建议数据库列长度为 255 个字符!

crypt_password

相关全局函数(描述见上文)。

( 返回 )

crypt_password( string $password ): string

示例

$password = 'foobar';

crypt_password( $password );

// $2y$10$6qKwbwTgwQNcmcaw04eSf.QpP3.4T0..bEnY62dd1ozM8L61nb8AC
  • 此方法为 静态参数:

返回值

加密后的密码。

isPassword

验证密码是否与加密密码匹配(CRYPT_BLOWFISH 算法)。

Util::isPassword( string $password, string $cryptedPassword ): boolean

is_password

相关全局函数(描述见上文)。

( 返回 )

is_password( string $password, string $cryptedPassword ): boolean

示例

$password = 'foobar';
$cryptedPassword = '$2y$10$6qKwbwTgwQNcmcaw04eSf.QpP3.4T0..bEnY62dd1ozM8L61nb8AC';

is_password( $password, $cryptedPassword );

// bool(true)
  • 此方法为 静态参数:

dd

转储给定变量的内容并退出脚本。

Util::dd( mixed $var )

dd

相关全局函数(描述见上文)。

( 返回 )

dd( mixed $var )

示例

$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

dd( $array );

// (
//     [foo] => bar
//     [baz] => qux
// )
  • 此方法为 静态参数:

dump

转储给定变量的内容。调用后脚本不会停止。

Util::dump( mixed $var )

dump

相关全局函数(描述见上文)。

( 返回 )

dump( mixed $var )

示例

$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

dump( $array );

// (
//     [foo] => bar
//     [baz] => qux
// )
  • 此方法为 静态参数:

Yml

辅助类,提供对常用 PHP yml 函数的轻松访问。

类 Yml

  • 全名:\BUROQ\Helper\Yml

isValidFile

验证给定的文件是否包含 yaml 语法。

Yml::isValidFile( string $file ): boolean

is_yml_file

相关全局函数(描述见上文)。

( 返回 )

is_yml_file( string $file ): boolean

示例

$file = /path/to/file.yml

is_yml_file( $file );

// bool(true)
  • 此方法为 静态参数:

返回值

如果文件包含 yaml 语法,则返回 true,否则返回 false。

isValid

测试给定字符串的语法是否为 yaml。

Yml::isValid( string $string ): boolean

is_yml

相关全局函数(描述见上文)。

( 返回 )

is_yml( string $string ): boolean

示例

$string = "
     foo: bar
     baz: qux
     foobar:
         foo: bar
";

is_yml( $string );

// bool(true)
  • 此方法为 静态参数:

返回值

如果字符串是 yaml,则返回 true,否则返回 false。

parse

将给定的 yaml 字符串转换为数组。

Yml::parse( string $yml ): array|null

yml_parse

相关全局函数(描述见上文)。

( 返回 )

yml_parse( string $yml ): array|null

示例

$yml = "
     foo: bar
     baz: qux
     foobar:
         foo: bar
";

yml_parse( $yml );

// (
//       [foo] => bar
//       [baz] => qux
//       [foobar] => (
//           [foo] => bar
//       )
// )
  • 此方法为 静态参数:

返回值

转换后的数组,错误时为 null。

get

使用点符号在 yaml 字符串中获取值。

Yml::get( string $key, string $yml ): mixed

yml_get

相关全局函数(描述见上文)。

( 返回 )

yml_get( string $key, string $yml ): mixed

示例

$yml = "
     foo: bar
     baz: qux
     foobar:
         foo: bar
";

yml_get( 'foobar.foo', $yml );

// bar
  • 此方法为 静态参数:

返回值

找到的值,否则为 null。

getFile

使用点符号在 yaml 文件中获取值。

Yml::getFile( string $key, string $ymlfile ): mixed

yml_get_file

相关全局函数(描述见上文)。

( 返回 )

yml_get_file( string $key, string $ymlfile ): mixed

示例

$ymlfile = '/path/to/file.yml';

yml_get_file( 'foobar.foo', $ymlfile );

// bar
  • 此方法为 静态参数:

返回值

找到的值,否则为 null。

parseFile

将 yaml 文件的内容加载到数组中。

Yml::parseFile( string $ymlfile ): array

yml_parse_file

相关全局函数(描述见上文)。

( 返回 )

yml_parse_file( string $ymlfile ): array|null

示例

$ymlfile = '/path/to/file.yml';

yml_parse_file( $ymlfile );

// (
//       [foo] => bar
//       [baz] => qux
//       [foobar] => (
//           [foo] => bar
//       )
// )
  • 此方法为 静态参数:

返回值

解析后的数组。

setFile

使用点符号在 yaml 文件中设置值。注意:文件中的所有注释将被删除!

Yml::setFile( string $key, mixed $value, string $ymlfile ): boolean

yml_set_file

相关全局函数(描述见上文)。

( 返回 )

yml_set_file( string $key, mixed $value, string $ymlfile ): boolean

示例

$ymlfile = '/path/to/file.yml';

yml_set_file( 'foobar.foo', 'baz', $ymlfile );

//   foo: bar
//   baz: qux
//   foobar:
//       foo: baz
  • 此方法为 静态参数:

返回值

如果值成功设置在 yaml 文件中,则返回 true,否则返回 false。

dumpFile

将给定的数组转换为 yaml 语法,并将其内容放入给定的文件中。注意:如果文件存在,则会被覆盖!

Yml::dumpFile( array|object $var, string $filename, integer $indent = 2, integer $wordwrap, boolean $openingDashes = false ): boolean

to_yml_file

相关全局函数(描述见上文)。

( 返回 )

to_yml_file( array|object $var, string $filename, int $indent = 2, int $wordwrap = 0, bool $openingDashes = false ): boolean

示例

$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

to_yml_file( $array, '/path/to/file.yml' );

//   foo: bar
//   baz: qux
  • 此方法为 静态参数:

返回值

成功则返回 true,否则返回 false。

dump

将给定的数组或对象转换为 yaml 字符串。

Yml::dump( array|object $var, integer $indent = 2, integer $wordwrap, boolean $openingDashes = false ): string|null

to_yml

相关全局函数(描述见上文)。

( 返回 )

to_yml( array|object $array, string $filename, int $indent = 2, int $wordwrap = 0, bool $openingDashes = false ): string|null

示例

$array = [
     'foo' => 'bar',
     'baz' => 'qux',
     'foobar' => [
         'foo' => 'bar'
     ]
];

to_yml( $array );

//   foo: bar
//   baz: qux
//   foobar:
//     foo: bar
  • 此方法为 静态参数:

返回值

转换后的 yaml 字符串。错误时返回 null。

set

使用点符号在 yaml 字符串中设置值。

Yml::set( string $key, mixed $value, string &$yml ): boolean

yml_set

相关全局函数(描述见上文)。

( 返回 )

yml_set( string $key, mixed $value, string &$yml ): boolean

示例

$yml = "
     foo: bar
     baz: qux
     foobar:
         foo: bar
";

yml_set( 'foobar.foo', 'baz', $yml );

//   foo: bar
//   baz: qux
//   foobar:
//       foo: baz
  • 此方法为 静态参数:

返回值

如果值成功设置,则返回 true,否则返回 false。

此文档于 2018-01-22 自动从源代码注释生成,使用 phpDocumentorcvuorinen/phpdoc-markdown-public