PHP code example of andreas-glaser / php-helpers

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

    

andreas-glaser / php-helpers example snippets


// Create a new instance with initial attributes
$attrs = AttributesHelper::f(['class' => 'btn']);

// Add more classes and attributes
$attrs->addClass('btn-primary')
     ->addStyle('margin', '10px')
     ->addData('toggle', 'modal');

// Render as HTML attributes string
echo $attrs; // outputs: class="btn btn-primary" style="margin:10px" data-toggle="modal"

use AndreasGlaser\Helpers\Html\BootstrapHelper;

// Basic glyphicon
echo BootstrapHelper::glyphIcon('home');
// Output: <span class="glyphicon glyphicon-home"></span>

// With additional attributes
echo BootstrapHelper::glyphIcon('search', [
    'id' => 'search-icon',
    'class' => 'icon-large text-primary',
    'title' => 'Search',
    'data-toggle' => 'tooltip'
]);
// Output: <span id="search-icon" class="icon-large text-primary glyphicon glyphicon-search" title="Search" data-toggle="tooltip"></span>

// Using AttributesHelper
$attrs = AttributesHelper::f()
    ->setId('my-icon')
    ->addClass('text-danger')
    ->addData('action', 'delete');
echo BootstrapHelper::glyphIcon('trash', $attrs);

use AndreasGlaser\Helpers\ArrayHelper;
use AndreasGlaser\Helpers\StringHelper;
use AndreasGlaser\Helpers\DateHelper;
use AndreasGlaser\Helpers\ValueHelper;
use AndreasGlaser\Helpers\CsvHelper;
use AndreasGlaser\Helpers\EmailHelper;
use AndreasGlaser\Helpers\Html\FormHelper;
use AndreasGlaser\Helpers\Html\AttributesHelper;
use AndreasGlaser\Helpers\Validate\Expect;
use AndreasGlaser\Helpers\Validate\IOExpect;

// Array operations
$array = ['user' => ['profile' => ['name' => 'John']]];
$name = ArrayHelper::getByPath($array, 'user.profile.name'); // Returns 'John'

// String operations
$string = 'Hello World';
$contains = StringHelper::contains($string, 'World'); // Returns true
$startsWith = StringHelper::startsWith($string, 'Hello'); // Returns true

// Date operations
$date = new DateTime();
$hours = DateHelper::diffHours($date, new DateTime('+1 day')); // Returns 24

// Value validation
$isValid = ValueHelper::isDateTime('2024-03-20'); // Returns true

// CSV operations
$csvData = [
    ['Name', 'Email', 'Age'],
    ['John Doe', '[email protected]', '30'],
    ['Jane Smith', '[email protected]', '25']
];
$csvString = CsvHelper::arrayToCsvString($csvData);
// Result: "Name,Email,Age\nJohn Doe,[email protected],30\nJane Smith,[email protected],25"

// Read CSV file to array
$data = CsvHelper::fileToArray('users.csv', true); // true for header row
// With custom delimiter
$data = CsvHelper::fileToArray('data.csv', false, 0, ';'); // semicolon delimiter

// Email operations
$validEmail = EmailHelper::isValid('[email protected]'); // Returns true
$invalidEmail = EmailHelper::isValid('invalid-email'); // Returns false

// Clean and normalize email addresses
$emails = '[email protected], invalid-email, [email protected]; [email protected]';
$cleanEmails = EmailHelper::clean($emails); // Returns ['[email protected]', '[email protected]', '[email protected]']

// Clean with custom delimiters
$emails = '[email protected]:[email protected]#[email protected]';
$cleanEmails = EmailHelper::clean($emails, [':', '#']); // Returns ['[email protected]', '[email protected]', '[email protected]']

// Clean array of emails
$emailArray = ['[email protected]', 'invalid', '[email protected]'];
$cleanEmails = EmailHelper::clean($emailArray); // Returns ['[email protected]', '[email protected]']

// Form generation
echo FormHelper::open('/users', 'POST', ['class' => 'user-form']);
echo FormHelper::text('name', 'John Doe', ['id' => 'name', '
bash
composer