1. Go to this page and download the library: Download jstewmc/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/ */
Num::bound(1, 10);
// returns 10, because 1, the number, is less than 10, the lower bound
Num::bound(10, 1, 5);
// returns 5, because 10, the number, is greater than 5, the upper bound
Str::password(8);
// returns a string like 'jNb^3#L@'
Str::password(8, ['upper' => 8]);
// returns a string like 'NBDRATCV', with exactly eight upper-case characters
Str::password(16, ['number' => 8]);
// returns a string like '*9f8F6b4F3f1:0/9', with at least eight numbers
Str::truncate('Lorem ipsum inum', 10);
// returns 'Lorem...', because the "u" in "ipsum" is the 10th character and
// the space after "Lorem" is the closest break character
Str::truncate('Lorem ipsum inum', 15);
// returns 'Lorem ipsum...', because the "u" in "inum" is the 15th character
Str::truncate('Lorem ipsum inum', 99);
// returns 'Lorem ipsum inum', because the string is shorter than the limit
// define a example class (for the purposes of this example, we'll define both a
// public property and a getter method)
class Example
{
public $property;
public function __construct(int $value)
{
$this->property = $value;
}
public function getProperty(): int
{
return $this->property;
}
}
$objects = [new Example(2), new Example(3), new Example(1)];
Arr::sortByProperty($objects, 'foo');
// returns (in pseudo-code) [{bar: 1}, {bar: 2}, {bar: 3}]
Arr::sortByMethod($objects, 'getBar');
// returns (in pseudo-code) [{bar: 1}, {bar: 2}, {bar: 3}]
$values = ['foo', 'bar', 'baz'];
Arr::inArray($values, 'f*'); // returns true, because of the leading "f" in "foo"
Arr::inArray($values, '*z'); // returns true, because of the trailing "z" in "baz"
Arr::inArray($values, '*a*'); // returns true, because of the "a" in "bar" and "baz"
$array1 = ['foo', 'bar', 'baz'];
$array2 = ['bar', 'qux'];
$actual = Arr::diff($array1, $array2);
$expected = [
['value' => 'foo', 'mask' => -1], // because "foo" should be deleted
['value' => 'bar', 'mask' => 0], // because "bar" should be unchanged
['value' => 'baz', 'mask' => -1], // because "baz" should be deleted
['value' => 'qux', 'mask' => 1] // because "qux" should be added
];
$actual == $expected; // returns true
$array1 = [0 => 'foo', 1 => 'bar'];
$array2 = [0 => 'foo', 1 => 'bar', 'baz' => 'qux'];
Arr::isAssoc($array1); // returns false
Arr::isAssoc($array2); // returns true, because there is a string key
$values = ['foo' => null, 'bar' => [], 'baz' => 1];
Arr::isEmpty('qux', $values);
// returns true, because the key "qux" does not exist
Arr::isEmpty('foo', $values);
// returns true, because the value of key "foo" is null
Arr::isEmpty('bar', $values);
// returns true, because the value of key "bar" is empty array
Arr::isEmpty('baz', $values);
// returns false, because the value of key "baz" is not empty
$array = ['foo' => 'bar', 'baz' => 'qux'];
Arr::keyStringReplace('f', 'g', $a);
// returns ['goo' => 'bar', 'baz' => 'qux'], because "f" was replaced with "g"
Arr::keyStringReplace('f', '', $a);
// returns ['oo' => 'bar', 'baz' => 'qux'], because "f" was replaced with ""
$directory = dirname(__FILE__).'/foo';
// The $container verifies you don't delete a directory outside of the target
// area accidentally. The path of the directory to be deleted MUST start with
// this path.
$container = dirname(__FILE__);
Dir::remove($directory, $container); // returns true