PHP code example of clausnz / php-helpers

1. Go to this page and download the library: Download clausnz/php-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clausnz / php-helpers example snippets




dump( 'any content' );



use CNZ\Helpers\Util as util;

util::dump( 'any content' );



 
 
use CNZ\Helpers\Dev as dev;

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

Arr::isAssoc( array $array ): boolean

is_assoc( array $array ): boolean

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

is_assoc( $array );

// bool(true)

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

to_object( array $array ): object|null

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

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

// baz

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

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

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

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


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

to_array( $var );

// (
//     [foo] => bar
// )

Arr::first( array $array ): mixed

array_first( array $array ): mixed

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

array_first( $array )

// bar

Arr::last( array $array ): mixed

array_last( array $array ): mixed

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

array_last( $array )

// qux

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

array_get( string key, array $array ): mixed

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

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

// foobar

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

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

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

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

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

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

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

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

Dev::isSmartphone(  ): boolean

is_smartphone(  ): boolean

if ( is_smartphone() ) {
     // I am a smartphone
}

Dev::isMobile(  ): boolean

is_mobile(  ): boolean

if ( is_mobile() ) {
     // I am a mobile device (smartphone/tablet or handheld)
}

Dev::mobileDetect(  ): \Detection\MobileDetect

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

// 8.1

Dev::isTablet(  ): boolean

is_tablet(  ): boolean

if ( is_tablet() ) {
     // I am a tablet
}

Dev::isDesktop(  ): boolean

is_desktop(  ): boolean

if ( is_desktop() ) {
     // I am a desktop computer (Mac, Linux, Windows)
}

Dev::isRobot(  ): boolean

is_robot(  ): boolean

if ( is_robot() ) {
     // I am a robot (search engine, bot, crawler, spider)
}

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

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

// Output the name of the bot that matched (if any)

Dev::isAndroid(  ): boolean

is_android(  ): boolean

if ( is_android() ) {
     // I am an Android based device
}

Dev::isIphone(  ): boolean

is_iphone(  ): boolean

if ( is_iphone() ) {
     // I am an iPhone
}

Dev::isSamsung(  ): boolean

is_samsung(  ): boolean

if ( is_samsung() ) {
     // I am a device from Samsung
}

Dev::isIOS(  ): boolean

is_ios(  ): boolean

if ( is_ios() ) {
     // I am an iOS based device
}

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

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.

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

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
// )

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

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

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

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

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

str_before( 'fox' $string );

// The quick brown

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

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...

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

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...

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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

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

str_after_last( '/' $path );

// image.jpg

Util::isEmail( string $email ): boolean

is_email( string $email ): boolean

$email = '[email protected]';

is_email( $email );

// bool(true)
bash
composer 
bash
composer 
php
Util::ip(  ): string|null
php
ip(  ): null|string
php
echo ip();

// 127.0.0.1
php
$password = 'foobar';

crypt_password( $password );

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

is_password( $password, $cryptedPassword );

// bool(true)
php
Util::dd( mixed $var )
php
dd( mixed $var )
php
$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

dd( $array );

// (
//     [foo] => bar
//     [baz] => qux
// )
php
Util::dump( mixed $var )
php
dump( mixed $var )
php
$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

dump( $array );

// (
//     [foo] => bar
//     [baz] => qux
// )
php
is_yml_file( string $file ): boolean
php
$file = /path/to/file.yml

is_yml_file( $file );

// bool(true)
php
is_yml( string $string ): boolean
php
$string = "
     foo: bar
     baz: qux
     foobar:
         foo: bar
";

is_yml( $string );

// bool(true)
php
Yml::parse( string $yml ): array|null
php
yml_parse( string $yml ): array|null
php
yml_get( string $key, string $yml ): mixed
php
yml_get_file( string $key, string $ymlfile ): mixed
php
$ymlfile = '/path/to/file.yml';

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

// bar
php
yml_parse_file( string $ymlfile ): array|null
php
yml_set_file( string $key, mixed $value, string $ymlfile ): boolean
php
$array = [
     'foo' => 'bar',
     'baz' => 'qux'
];

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

//   foo: bar
//   baz: qux
php
Yml::set( string $key, mixed $value, string &$yml ): boolean
php
yml_set( string $key, mixed $value, string &$yml ): boolean