PHP code example of lithephp / swisshelper

1. Go to this page and download the library: Download lithephp/swisshelper 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/ */

    

lithephp / swisshelper example snippets


// Get current DateTime object
$datetime = now();

// Get formatted current date
$formatted = now('Y-m-d H:i:s');
$date = now('Y-m-d');
$time = now('H:i:s');

$slug = str('Hello World!')->slug();
// Output: "hello-world"

$slug = str('Café com leite')->slug();
// Output: "cafe-com-leite"

$text = str('Café à la crème')->removeAccents();
// Output: "Cafe a la creme"

$numbers = str('Phone: (123) 456-7890')->onlyNumbers();
// Output: "1234567890"

$numbers = str('Order #123-456')->onlyNumbers();
// Output: "123456"

// CPF (Brazilian ID)
$masked = str('12345678901')->mask('###.###.###-##');
// Output: "123.456.789-01"

// Phone number
$masked = str('1234567890')->mask('(##) ####-####');
// Output: "(12) 3456-7890"

// Custom mask
$masked = str('ABC123')->mask('???-###');
// Output: "ABC-123"

$array = [
    'user' => [
        'profile' => [
            'name' => 'John Doe',
            'settings' => [
                'theme' => 'dark'
            ]
        ]
    ]
];

// Access with dot notation
$name = arr($array)->get('user.profile.name');
// Output: "John Doe"

// With default value
$color = arr($array)->get('user.profile.color', 'blue');
// Output: "blue"

$array = ['name' => 'John', 'age' => 30, 'email' => '[email protected]'];

// Get only specific keys
$only = arr($array)->only(['name', 'email']);
// Output: ['name' => 'John', 'email' => '[email protected]']

// Get everything except specified keys
$except = arr($array)->except(['age']);
// Output: ['name' => 'John', 'email' => '[email protected]']

validate('[email protected]')->email(); // true
validate('invalid-email')->email(); // false

validate('https://example.com')->url(); // true
validate('invalid-url')->url(); // false

validate('192.168.1.1')->ip(); // true
validate('256.256.256.256')->ip(); // false

validate('2024-01-03')->date(); // true
validate('2024-13-45')->date(); // false

// Custom format
validate('03/01/2024')->date('d/m/Y'); // true

validate('John Doe')->name(); // true
validate('John123')->name(); // false

validate('4532015112830366')->creditCard(); // true
validate('1234567890123456')->creditCard(); // false

// Validate password strength
validate('P@ssw0rd')->password(); // true
validate('weak')->password(); // false

// Custom rules
validate('1234Abcd!')->password(8, false, true, true, true); // true

// Check if age is within a range
validate('2000-01-01')->age(18, 60); // true
validate('2010-01-01')->age(18); // false

// Check if string or numeric value is between limits
validate('Hello')->between(3, 10); // true
validate(100)->between(50, 150); // true

// Check if string contains a specific substring
validate('Hello World')->contains('World'); // true

// Check if string starts or ends with specific text
validate('example.com')->startsWith('example'); // true
validate('example.com')->endsWith('.com'); // true

// Generate a random alphanumeric string of 16 characters
$alnum = random(16);

// Generate a random alphabetic string of 10 characters
$alpha = random(10, 'alpha');

// Generate a random numeric string of 8 characters
$numeric = random(8, 'numeric');

// Generate a random non-zero numeric string of 6 characters
$nozero = random(6, 'nozero');

// Get the current URL
$current = url()->current();

// Get the base URL
$base = url()->base();

// Generate a URL to a specific path
$path = url()->to('path/to/resource');

// Get the previous URL (referer)
$previous = url()->previous();

// Set a session variable
session()->put('key', 'value');

// Get a session variable
$value = session()->get('key');

// Check if a session variable exists
$exists = session()->has('key');

// Get all session variables
$all = session()->all();

// Unset a session variable
session()->forget('key');

// Destroy the session
session()->destroy();

$token = csrf_token();

$field = csrf_field();
// Output: <input type="hidden" name="_token" value="your_csrf_token_here">