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]']

// Basic formatting
echo money(1234.56); // "$1,234.56"
echo money(1234.56, 'EUR'); // "1.234,56€"
echo money(1234.56, 'BRL'); // "R$ 1.234,56"

// Custom formatting options
echo money(1234.56, 'USD', [
    'decimals' => 0,
    'symbol' => false
]); // "1,235"

// Supported currencies
// USD - United States Dollar
// EUR - Euro
// GBP - British Pound
// JPY - Japanese Yen
// CNY - Chinese Yuan
// BRL - Brazilian Real
// INR - Indian Rupee
// RUB - Russian Ruble
// AUD - Australian Dollar
// CAD - Canadian Dollar
// AOA - Angolan Kwanza

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('Abc123!@#')->password(
    minLength: 8,
    equireLower: true
); // true

// Customize alse
); // true

validate('1990-01-01')->age(min: 18, max: 65); // true
validate('2020-01-01')->age(min: 18); // false

// Between validation
validate('test')->between(2, 5); // true (string length)
validate(10)->between(1, 100); // true (numeric value)

// Contains validation
validate('Hello World')->contains('World'); // true

// Starts/Ends With validation
validate('Hello World')->startsWith('Hello'); // true
validate('Hello World')->endsWith('World'); // true

// Integer validation
validate('123')->int(); // true
validate('12.3')->int(); // false

// Default (16 characters, alphanumeric)
echo random(); // "a1B2c3D4e5F6g7H8"

// Specific length
echo random(8); // "Xa4Kp9Yz"

// Only letters
echo random(8, 'alpha'); // "AbCdEfGh"

// Only numbers
echo random(6, 'numeric'); // "123456"

// Numbers without zero
echo random(4, 'nozero'); // "1234"

// Get current URL
echo url()->current();
// Output: "https://example.com/current/path"

// Get base URL
echo url()->base();
// Output: "https://example.com"

// Generate URL
echo url()->to('products/1');
// Output: "https://example.com/products/1"

// Get previous URL
echo url()->previous();
// Output: Returns the previous URL or base URL if not available