clausnz / php-helpers
一组有用的 PHP 辅助函数。
Requires
- php: >=5.6
- jaybizzle/crawler-detect: ^1.2
- mobiledetect/mobiledetectlib: ^2.8
- mustangostang/spyc: ^0.6
Requires (Dev)
- cvuorinen/phpdoc-markdown-public: ^0.2
- mikey179/vfsstream: ^1.5.0
- phpunit/phpunit: ^5
This package is not auto-updated.
Last update: 2024-09-29 05:14:51 UTC
README
关于
clausnz/php-helpers
库是一组 45 个有用的 PHP 辅助函数(PHP 5.6, 7.*)。
使用 composer
安装后,全局函数可以在代码的任何地方访问。
安装
composer require clausnz/php-helpers
示例
<?php dump( 'any content' );
如果项目定义的函数列表(内置和用户定义)中已经存在同名函数,则该函数将不会在您的环境中注册。因此,不会出现与现有函数的冲突。
然而,每个函数仍然可以通过适当的使用语句以静态方式访问。
示例
<?php use CNZ\Helpers\Util as util; util::dump( 'any content' );
致谢
该库使用了以下出色的知名库
- https://github.com/serbanghita/Mobile-Detect
- https://github.com/JayBizzle/Crawler-Detect
- https://github.com/mustangostang/spyc
测试
安装
使用 composer 安装最新的 clausnz/php-helper
库
composer require clausnz/php-helpers
同时确保需要 composer 自动加载文件
require __DIR__ . '/vendor/autoload.php';
安装后,新的全局 PHP 函数可以在代码的任何地方使用。要访问辅助类中的(几乎相同)静态函数,请将适当的使用语句添加到您的文件中
示例
<?php use CNZ\Helpers\Dev as dev; if( dev::isIphone() ) { // Do something here }
可用的 PHP 函数
目录
API 文档
目录
Arr
提供方便访问有用 PHP 数组函数的辅助类。
类 Arr
- 全名:\CNZ\Helpers\Arr
isAssoc
检测给定值是否为关联数组。
Arr::isAssoc( array $array ): boolean
is_assoc
相关全局函数(描述见上方)。
(返回)
is_assoc( array $array ): boolean
示例
$array = [ 'foo' => 'bar' ]; is_assoc( $array ); // bool(true)
- 此方法为 静态。 参数:
返回值
如果数组是关联的,则为true,否则为false。
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 // ] // )
- 此方法为 静态。 参数:
返回值
如果成功设置新值,则为true,否则为false。
Dev
辅助类,提供对与用户代理结合使用的有用PHP函数的便捷访问。
类Dev
- 全名:\CNZ\Helpers\Dev
isSmartphone
确定当前设备是否是智能手机。
Dev::isSmartphone( ): boolean
is_smartphone
相关全局函数(描述见上方)。
(返回)
is_smartphone( ): boolean
示例
if ( is_smartphone() ) { // I am a smartphone }
- 此方法为静态。
返回值
如果当前访问者使用智能手机,则为true,否则为false。
isMobile
检测当前访问者是否使用移动设备(智能手机/平板电脑/手持设备)。
Dev::isMobile( ): boolean
is_mobile
相关全局函数(描述见上方)。
(返回)
is_mobile( ): boolean
示例
if ( is_mobile() ) { // I am a mobile device (smartphone/tablet or handheld) }
- 此方法为静态。
返回值
如果当前访问者使用移动设备,则为true,否则为false。
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 }
- 此方法为静态。
返回值
如果当前访问者使用平板电脑设备,则为true,否则为false。
isDesktop
确定当前访问者是否使用台式计算机。
Dev::isDesktop( ): boolean
is_desktop
相关全局函数(描述见上方)。
(返回)
is_desktop( ): boolean
示例
if ( is_desktop() ) { // I am a desktop computer (Mac, Linux, Windows) }
- 此方法为静态。
返回值
如果当前访问者使用台式计算机,则为true,否则为false。
isRobot
确定当前访问者是否是搜索引擎/机器人/爬虫/蜘蛛。
Dev::isRobot( ): boolean
is_robot
相关全局函数(描述见上方)。
(返回)
is_robot( ): boolean
示例
if ( is_robot() ) { // I am a robot (search engine, bot, crawler, spider) }
- 此方法为静态。
返回值
如果当前访问者是搜索引擎/机器人/爬虫/蜘蛛,则为true,否则为false。
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的设备,则为true,否则为false。
isIphone
确定当前设备是否是iPhone。
Dev::isIphone( ): boolean
is_iphone
相关全局函数(描述见上方)。
(返回)
is_iphone( ): boolean
示例
if ( is_iphone() ) { // I am an iPhone }
- 此方法为静态。
返回值
如果当前访问者使用iPhone,则为true,否则为false。
isSamsung
确定当前设备是否来自Samsung。
Dev::isSamsung( ): boolean
is_samsung
相关全局函数(描述见上方)。
(返回)
is_samsung( ): boolean
示例
if ( is_samsung() ) { // I am a device from Samsung }
- 此方法为静态。
返回值
如果当前访问者使用Samsung设备,则为true,否则为false。
isIOS
确定当前设备是否运行iOS操作系统。
Dev::isIOS( ): boolean
is_ios
相关全局函数(描述见上方)。
(返回)
is_ios( ): boolean
示例
if ( is_ios() ) { // I am an iOS based device }
- 此方法为静态。
返回值
如果当前访问者使用iOS设备,则为true,否则为false。
Str
辅助类,提供对有用PHP字符串函数的便捷访问。
类Str
- 全名:\CNZ\Helpers\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,则为真,否则为假。
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,则为真,否则为假。
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,则为真,否则为假。
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,则为真,否则为假。
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
- 全名:\CNZ\Helpers\Util
isEmail
验证给定的电子邮件地址。
Util::isEmail( string $email ): boolean
is_email
相关全局函数(描述见上方)。
(返回)
is_email( string $email ): boolean
示例
$email = 'foobar@example.com'; is_email( $email ); // bool(true)
- 此方法为 静态。 参数:
返回值
如果给定字符串是有效的电子邮件地址,则为真,否则为假。
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
- 全名:\CNZ\Helpers\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语法,则为真,否则为假。
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,则为真,否则为假。
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文件中成功设置,则为真,否则为假。
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
- 此方法为 静态。 参数:
返回值
成功时为真,否则为假。
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
- 此方法为 静态。 参数:
返回值
如果值成功设置,则为真,否则为假。
此文档于2018-01-22自动从源代码注释生成,使用phpDocumentor和cvuorinen/phpdoc-markdown-public。