2017 © Pedro Peláez
 

library php-helpers

A Collection of useful php helper functions.

image

clausnz/php-helpers

A Collection of useful php helper functions.

  • Monday, January 22, 2018
  • by clausnz
  • Repository
  • 0 Watchers
  • 2 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

About

The library clausnz/php-helpers is a collection of 45 useful php helper functions (PHP 5.6, 7.*).
After installation with composer, the global functions are accessable from everywhere in your code:, (*2)

Installation

composer require clausnz/php-helpers

Example

<?php

dump( 'any content' );

If a function with the same name already exists in the list of your project's defined functions ( built-in and user-defined ), it will simply not be registered in your environment. Therefore, no conflicts with existing functions will appear., (*3)

Nevertheless, every function is still accessable it in a static way with the proper use-statement:, (*4)

Example

<?php

use CNZ\Helpers\Util as util;

util::dump( 'any content' );

CREDITS

This library makes use of the following brilliant and well known libraries:, (*5)

  • https://github.com/serbanghita/Mobile-Detect
  • https://github.com/JayBizzle/Crawler-Detect
  • https://github.com/mustangostang/spyc

Tests

All functions are tested against a number of unit tests and PHP Versions. Build Status, (*6)

Install

Install the latest clausnz/php-helper library with composer:, (*7)

composer require clausnz/php-helpers

Also make sure to require your composer autoload file:, (*8)

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

After installation, the new global PHP functions are available everywhere in your code. To access the ( almost identical ) static functions in the helper classes, add the proper use statement to your file:, (*9)

Example

```php <?php, (*10)

use CNZ\Helpers\Dev as dev;, (*11)

if( dev::isIphone() ) { // Do something here } ``` # Available PHP Functions, (*12)

Table of Contents

API Documentation

Table of Contents

Arr

Helper class that provides easy access to useful php array functions., (*13)

Class Arr, (*14)

  • Full name: \CNZ\Helpers\Arr

isAssoc

Detects if the given value is an associative array., (*15)

Arr::isAssoc( array $array ): boolean

is_assoc

Related global function (description see above)., (*16)

( jump back )

is_assoc( array $array ): boolean

Example

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

is_assoc( $array );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$array array Any type of array.

Return Value:, (*17)

True if the array is associative, false otherwise., (*18)


toObject

Converts an array to an object., (*19)

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

to_object

Related global function (description see above)., (*20)

( jump back )

to_object( array $array ): object|null

Example

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

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

// baz
  • This method is static. Parameters:
Parameter Type Description
$array array The array to be converted.

Return Value:, (*21)

A std object representation of the converted array., (*22)


dump

Converts a string or an object to an array., (*23)

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

to_array

Related global function (description see above)., (*24)

( jump back )

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

Example 1 (string)

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

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

Example 2 (object)

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

to_array( $var );

// (
//     [foo] => bar
// )
  • This method is static. Parameters:
Parameter Type Description
$var string|object String or object.

Return Value:, (*25)

An array representation of the converted string or object. Returns null on error., (*26)


first

Returns the first element of an array., (*27)

Arr::first( array $array ): mixed

array_first

Related global function (description see above)., (*28)

( jump back )

array_first( array $array ): mixed

Example

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

array_first( $array )

// bar
  • This method is static. Parameters:
Parameter Type Description
$array array The concerned array.

Return Value:, (*29)

The value of the first element, without key. Mixed type., (*30)


last

Returns the last element of an array., (*31)

Arr::last( array $array ): mixed

array_last

Related global function (description see above)., (*32)

( jump back )

array_last( array $array ): mixed

Example

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

array_last( $array )

// qux
  • This method is static. Parameters:
Parameter Type Description
$array array The concerned array.

Return Value:, (*33)

The value of the last element, without key. Mixed type., (*34)


get

Gets a value in an array by dot notation for the keys., (*35)

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

array_get

Related global function (description see above)., (*36)

( jump back )

array_get( string key, array $array ): mixed

Example

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

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

// foobar
  • This method is static. Parameters:
Parameter Type Description
$key string The key by dot notation.
$array array The array to search in.

Return Value:, (*37)

The searched value, null otherwise., (*38)


set

Sets a value in an array using the dot notation., (*39)

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

array_set

Related global function (description see above)., (*40)

( jump back )

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

Example 1

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

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

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

Example 2

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

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

// (
//     [foo] => bar
//     [baz] => [
//         [qux] => bazqux
//         [foo] => bar
//     ]
// )
  • This method is static. Parameters:
Parameter Type Description
$key string The key to set using dot notation.
$value mixed The value to set on the specified key.
$array array The concerned array.

Return Value:, (*41)

True if the new value was successfully set, false otherwise., (*42)


Dev

Helper class that provides easy access to useful php functions in conjunction with the user agent., (*43)

Class Dev, (*44)

  • Full name: \CNZ\Helpers\Dev

isSmartphone

Determes if the current device is a smartphone., (*45)

Dev::isSmartphone(  ): boolean

is_smartphone

Related global function (description see above)., (*46)

( jump back )

is_smartphone(  ): boolean

Example

if ( is_smartphone() ) {
     // I am a smartphone
}
  • This method is static.

Return Value:, (*47)

True if current visitor uses a smartphone, false otherwise., (*48)


isMobile

Detects if the current visitor uses a mobile device (Smartphone/Tablet/Handheld)., (*49)

Dev::isMobile(  ): boolean

is_mobile

Related global function (description see above)., (*50)

( jump back )

is_mobile(  ): boolean

Example

if ( is_mobile() ) {
     // I am a mobile device (smartphone/tablet or handheld)
}
  • This method is static.

Return Value:, (*51)

True if current visitor uses a mobile device, false otherwise., (*52)


mobileDetect

Get a singleton MobileDetect object to call every method it provides., (*53)

Dev::mobileDetect(  ): \Detection\MobileDetect

Public access for use of outside this class. Mobile_Detect doku: https://github.com/serbanghita/Mobile-Detect, (*54)

This method has no related global function!, (*55)

( jump back )

Example

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

// 8.1
  • This method is static.

Return Value:, (*56)

A singleton MobileDetect object to call every method it provides., (*57)


isTablet

Determes if the current visitor uses a tablet device., (*58)

Dev::isTablet(  ): boolean

is_tablet

Related global function (description see above)., (*59)

( jump back )

is_tablet(  ): boolean

Example

if ( is_tablet() ) {
     // I am a tablet
}
  • This method is static.

Return Value:, (*60)

True if current visitor uses a tablet device, false otherwise., (*61)


isDesktop

Determes if the current visitor uses a desktop computer., (*62)

Dev::isDesktop(  ): boolean

is_desktop

Related global function (description see above)., (*63)

( jump back )

is_desktop(  ): boolean

Example

if ( is_desktop() ) {
     // I am a desktop computer (Mac, Linux, Windows)
}
  • This method is static.

Return Value:, (*64)

True if current visitor uses a desktop computer, false otherwise., (*65)


isRobot

Determes if the current visitor is a search engine/bot/crawler/spider., (*66)

Dev::isRobot(  ): boolean

is_robot

Related global function (description see above)., (*67)

( jump back )

is_robot(  ): boolean

Example

if ( is_robot() ) {
     // I am a robot (search engine, bot, crawler, spider)
}
  • This method is static.

Return Value:, (*68)

True if the current visitor is a search engine/bot/crawler/spider, false otherwise., (*69)


crawlerDetect

Get a singleton CrawlerDetect object to call every method it provides., (*70)

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

Public access for use of outside this class. Crawler-Detect doku: https://github.com/JayBizzle/Crawler-Detect, (*71)

This method has no related global function!, (*72)

( jump back )

Example

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

// Output the name of the bot that matched (if any)
  • This method is static.

isAndroid

Determes if the current device is running an Android operating system., (*73)

Dev::isAndroid(  ): boolean

is_android

Related global function (description see above)., (*74)

( jump back )

is_android(  ): boolean

Example

if ( is_android() ) {
     // I am an Android based device
}
  • This method is static.

Return Value:, (*75)

True if current visitor uses an Android based device, false otherwise., (*76)


isIphone

Determes if the current device is an iPhone., (*77)

Dev::isIphone(  ): boolean

is_iphone

Related global function (description see above)., (*78)

( jump back )

is_iphone(  ): boolean

Example

if ( is_iphone() ) {
     // I am an iPhone
}
  • This method is static.

Return Value:, (*79)

True if current visitor uses an iPhone, false otherwise., (*80)


isSamsung

Determes if the current device is from Samsung., (*81)

Dev::isSamsung(  ): boolean

is_samsung

Related global function (description see above)., (*82)

( jump back )

is_samsung(  ): boolean

Example

if ( is_samsung() ) {
     // I am a device from Samsung
}
  • This method is static.

Return Value:, (*83)

True if current visitor uses a Samsung device, false otherwise., (*84)


isIOS

Determes if the current device is running an iOS operating system., (*85)

Dev::isIOS(  ): boolean

is_ios

Related global function (description see above)., (*86)

( jump back )

is_ios(  ): boolean

Example

if ( is_ios() ) {
     // I am an iOS based device
}
  • This method is static.

Return Value:, (*87)

True if current visitor uses an iOS device, false otherwise., (*88)


Str

Helper class that provides easy access to useful php string functions., (*89)

Class Str, (*90)

  • Full name: \CNZ\Helpers\Str

insert

Inserts one or more strings into another string on a defined position., (*91)

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

str_insert

Related global function (description see above)., (*92)

( jump back )

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

Example

$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.
  • This method is static. Parameters:
Parameter Type Description
$keyValue array An associative array with key => value pairs.
$string string The text with the strings to be replaced.

Return Value:, (*93)

The replaced string., (*94)


between

Return the content in a string between a left and right element., (*95)

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

str_between

Related global function (description see above)., (*96)

( jump back )

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

Example

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

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

// (
//     [0] => foo
//     [1] => bar
// )
  • This method is static. Parameters:
Parameter Type Description
$left string The left element of the string to search.
$right string The right element of the string to search.
$string string The string to search in.

Return Value:, (*97)

A result array with all matches of the search., (*98)


after

Return the part of a string after a given value., (*99)

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

str_after

Related global function (description see above)., (*100)

( jump back )

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

Example

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

str_after( 'fox' $string );

// jumps over the lazy dog
  • This method is static. Parameters:
Parameter Type Description
$search string The string to search for.
$string string The string to search in.

Return Value:, (*101)

The found string after the search string. Whitespaces at beginning will be removed., (*102)


before

Get the part of a string before a given value., (*103)

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

str_before

Related global function (description see above)., (*104)

( jump back )

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

Example

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

str_before( 'fox' $string );

// The quick brown
  • This method is static. Parameters:
Parameter Type Description
$search string The string to search for.
$string string The string to search in.

Return Value:, (*105)

The found string before the search string. Whitespaces at end will be removed., (*106)


limitWords

Limit the number of words in a string. Put value of $end to the string end., (*107)

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

str_limit_words

Related global function (description see above)., (*108)

( jump back )

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

Example

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

str_limit_words( $string, 3 );

// The quick brown...
  • This method is static. Parameters:
Parameter Type Description
$string string The string to limit the words.
$limit integer The number of words to limit. Defaults to 10.
$end string The string to end the cut string. Defaults to '...'

Return Value:, (*109)

The limited string with $end at the end., (*110)


limit

Limit the number of characters in a string. Put value of $end to the string end., (*111)

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

str_limit

Related global function (description see above)., (*112)

( jump back )

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

Example

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

str_limit( $string, 15 );

// The quick brown...
  • This method is static. Parameters:
Parameter Type Description
$string string The string to limit the characters.
$limit integer The number of characters to limit. Defaults to 100.
$end string The string to end the cut string. Defaults to '...'

Return Value:, (*113)

The limited string with $end at the end., (*114)


contains

Tests if a string contains a given element, (*115)

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

str_contains

Related global function (description see above)., (*116)

( jump back )

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

Example

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

str_contains( $array, $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$needle string|array A string or an array of strings.
$haystack string The string to search in.

Return Value:, (*117)

True if $needle is found, false otherwise., (*118)


containsIgnoreCase

Tests if a string contains a given element. Ignore case sensitivity., (*119)

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

str_icontains

Related global function (description see above)., (*120)

( jump back )

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

Example

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

str_icontains( $array, $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$needle string|array A string or an array of strings.
$haystack string The string to search in.

Return Value:, (*121)

True if $needle is found, false otherwise., (*122)


startsWith

Determine if a given string starts with a given substring., (*123)

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

str_starts_with

Related global function (description see above)., (*124)

( jump back )

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

Example

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

str_starts_with( $array, $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$needle string|array The string or array of strings to search for.
$haystack string The string to search in.

Return Value:, (*125)

True if $needle was found, false otherwise., (*126)


startsWithIgnoreCase

Determine if a given string starts with a given substring. Ignore case sensitivity., (*127)

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

str_istarts_with

Related global function (description see above)., (*128)

( jump back )

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

Example

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

str_istarts_with( $array, $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$needle string|array The string or array of strings to search for.
$haystack string The string to search in.

Return Value:, (*129)

True if $needle was found, false otherwise., (*130)


endsWith

Determine if a given string ends with a given substring., (*131)

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

str_ends_with

Related global function (description see above)., (*132)

( jump back )

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

Example

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

str_ends_with( $array, $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$needle string|array The string or array of strings to search for.
$haystack string The string to search in.

Return Value:, (*133)

True if $needle was found, false otherwise., (*134)


endsWithIgnoreCase

Determine if a given string ends with a given substring., (*135)

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

str_iends_with

Related global function (description see above)., (*136)

( jump back )

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

Example

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

str_iends_with( $array, $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$needle string|array The string or array of strings to search for.
$haystack string The string to search in.

Return Value:, (*137)

True if $needle was found, false otherwise., (*138)


afterLast

Return the part of a string after the last occurrence of a given search value., (*139)

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

str_after_last

Related global function (description see above)., (*140)

( jump back )

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

Example

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

str_after_last( '/' $path );

// image.jpg
  • This method is static. Parameters:
Parameter Type Description
$search string The string to search for.
$string string The string to search in.

Return Value:, (*141)

The found string after the last occurrence of the search string. Whitespaces at beginning will be removed., (*142)


Util

Helper class that provides easy access to useful common php functions., (*143)

Class Util, (*144)

  • Full name: \CNZ\Helpers\Util

isEmail

Validate a given email address., (*145)

Util::isEmail( string $email ): boolean

is_email

Related global function (description see above)., (*146)

( jump back )

is_email( string $email ): boolean

Example

$email = 'foobar@example.com';

is_email( $email );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$email string The email address to test.

Return Value:, (*147)

True if given string is a valid email address, false otherwise., (*148)


ip

Get the current ip address of the user., (*149)

Util::ip(  ): string|null

user_ip

Related global function (description see above)., (*150)

( jump back )

ip(  ): null|string

Example

echo ip();

// 127.0.0.1
  • This method is static.

Return Value:, (*151)

The detected ip address, null if the ip was not detected., (*152)


cryptPassword

Creates a secure hash from a given password. Uses the CRYPT_BLOWFISH algorithm., (*153)

Util::cryptPassword( string $password ): string

Note: 255 characters for database column recommended!, (*154)

crypt_password

Related global function (description see above)., (*155)

( jump back )

crypt_password( string $password ): string

Example

$password = 'foobar';

crypt_password( $password );

// $2y$10$6qKwbwTgwQNcmcaw04eSf.QpP3.4T0..bEnY62dd1ozM8L61nb8AC
  • This method is static. Parameters:
Parameter Type Description
$password string The password to crypt.

Return Value:, (*156)

The crypted password., (*157)


isPassword

Verifies that a password matches a crypted password (CRYPT_BLOWFISH algorithm)., (*158)

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

is_password

Related global function (description see above)., (*159)

( jump back )

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

Example

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

is_password( $password, $cryptedPassword );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$password string The password to test.
$cryptedPassword string The crypted password (e.g. stored in the database).

dd

Dumps the content of the given variable and exits the script., (*160)

Util::dd( mixed $var )

dd

Related global function (description see above)., (*161)

( jump back )

dd( mixed $var )

Example

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

dd( $array );

// (
//     [foo] => bar
//     [baz] => qux
// )
  • This method is static. Parameters:
Parameter Type Description
$var mixed The var to dump.

dump

Dumps the content of the given variable. Script does NOT stop after call., (*162)

Util::dump( mixed $var )

dump

Related global function (description see above)., (*163)

( jump back )

dump( mixed $var )

Example

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

dump( $array );

// (
//     [foo] => bar
//     [baz] => qux
// )
  • This method is static. Parameters:
Parameter Type Description
$var mixed The var to dump.

Yml

Helper class that provides easy access to useful php yml functions., (*164)

Class Yml, (*165)

  • Full name: \CNZ\Helpers\Yml

isValidFile

Validates if a given file contains yaml syntax., (*166)

Yml::isValidFile( string $file ): boolean

is_yml_file

Related global function (description see above)., (*167)

( jump back )

is_yml_file( string $file ): boolean

Example

$file = /path/to/file.yml

is_yml_file( $file );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$file string The file to test for yaml syntax.

Return Value:, (*168)

True if the file contains yaml syntax, false otherwise., (*169)


isValid

Tests if the syntax of a given string is yaml., (*170)

Yml::isValid( string $string ): boolean

is_yml

Related global function (description see above)., (*171)

( jump back )

is_yml( string $string ): boolean

Example

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

is_yml( $string );

// bool(true)
  • This method is static. Parameters:
Parameter Type Description
$string string The string to test for yaml syntax.

Return Value:, (*172)

True if the string is yaml, false otherwise., (*173)


parse

Transforms a given yaml string into an array., (*174)

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

yml_parse

Related global function (description see above)., (*175)

( jump back )

yml_parse( string $yml ): array|null

Example

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

yml_parse( $yml );

// (
//       [foo] => bar
//       [baz] => qux
//       [foobar] => (
//           [foo] => bar
//       )
// )
  • This method is static. Parameters:
Parameter Type Description
$yml string The yaml string to parse.

Return Value:, (*176)

The transformed array, null on error., (*177)


get

Gets a value in a yaml string using the dot notation., (*178)

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

yml_get

Related global function (description see above)., (*179)

( jump back )

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

Example

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

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

// bar
  • This method is static. Parameters:
Parameter Type Description
$key string The key to search using dot notation (e.g. 'foo.bar.baz').
$yml string The yml string to search in.

Return Value:, (*180)

The found value, null otherwise., (*181)


getFile

Gets a value in a yaml file using the dot notation., (*182)

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

yml_get_file

Related global function (description see above)., (*183)

( jump back )

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

Example

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

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

// bar
  • This method is static. Parameters:
Parameter Type Description
$key string The key to search using dot notation (e.g. 'foo.bar.baz').
$ymlfile string The ymlfile to search in.

Return Value:, (*184)

The found value, null otherwise., (*185)


parseFile

Loads the content of a yamlfile into an array., (*186)

Yml::parseFile( string $ymlfile ): array

yml_parse_file

Related global function (description see above)., (*187)

( jump back )

yml_parse_file( string $ymlfile ): array|null

Example

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

yml_parse_file( $ymlfile );

// (
//       [foo] => bar
//       [baz] => qux
//       [foobar] => (
//           [foo] => bar
//       )
// )
  • This method is static. Parameters:
Parameter Type Description
$ymlfile string The path of the file to read from.

Return Value:, (*188)

The parsed array., (*189)


setFile

Sets a value in a yamlfile using the dot notation. Note: all comments in the file will be removed!, (*190)

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

yml_set_file

Related global function (description see above)., (*191)

( jump back )

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

Example

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

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

//   foo: bar
//   baz: qux
//   foobar:
//       foo: baz
  • This method is static. Parameters:
Parameter Type Description
$key string The string to search with dot notation
$value mixed The value to set on the specified key.
$ymlfile string The ymlfile to set the value in.

Return Value:, (*192)

True if value was successfully set in yamlfile, false otherwise., (*193)


dumpFile

Transformes a given array to yaml syntax and puts its content into a given file. Note: if the file exists, it will be overwritten!, (*194)

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

to_yml_file

Related global function (description see above)., (*195)

( jump back )

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

Example

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

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

//   foo: bar
//   baz: qux
  • This method is static. Parameters:
Parameter Type Description
$var array|object The array or object to transform.
$filename string The path to the file to write the yaml string into. Note: if the file already exists, it will be overwritten!
$indent integer The indent of the converted yaml. Defaults to 2.
$wordwrap integer After the given number a string will be wraped. Default to 0 (no wordwrap).
$openingDashes boolean True if the yaml string should start with opening dashes. Defaults to false.

Return Value:, (*196)

True on success, false otherwise., (*197)


dump

Transformes a given array or object to a yaml string., (*198)

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

to_yml

Related global function (description see above)., (*199)

( jump back )

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

Example

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

to_yml( $array );

//   foo: bar
//   baz: qux
//   foobar:
//     foo: bar
  • This method is static. Parameters:
Parameter Type Description
$var array|object The array or object to transform.
$indent integer The indent of the converted yaml. Defaults to 2.
$wordwrap integer After the given number a string will be wraped. Default to 0 (no wordwrap).
$openingDashes boolean True if the yaml string should start with opening dashes. Defaults to false.

Return Value:, (*200)

The converted yaml string. On errors, null is returned., (*201)


set

Sets a value in a yaml string using the dot notation., (*202)

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

yml_set

Related global function (description see above)., (*203)

( jump back )

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

Example

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

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

//   foo: bar
//   baz: qux
//   foobar:
//       foo: baz
  • This method is static. Parameters:
Parameter Type Description
$key string The string to search with dot notation
$value mixed The value to set on the specified key.
$yml string The yml string to search in. Note: all comments in the string will be removed!

Return Value:, (*204)

True if value was successfully set, false otherwise., (*205)



This document was automatically generated from source code comments on 2018-01-22 using phpDocumentor and cvuorinen/phpdoc-markdown-public, (*206)

The Versions

22/01 2018

dev-master

9999999-dev

A Collection of useful php helper functions.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Claus Bayer

function php helper array yaml string utilities device

22/01 2018

0.2.1

0.2.1.0

A Collection of useful php helper functions.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Claus Bayer

function php helper array yaml string utilities device

21/01 2018

0.2

0.2.0.0

A Collection of useful php helper functions.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Claus Bayer

function php helper array yaml string utilities device