PHP code example of makis83 / helpers

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

    

makis83 / helpers example snippets


// Valid JSON
$isJson = Data::isJson('{"key": "value"}'); // true
$isJson = Data::isJson('["item1", "item2"]'); // true
$isJson = Data::isJson('123'); // true

// Invalid JSON
$isJson = Data::isJson('This is not JSON.'); // false
$isJson = Data::isJson('{"key": "value"'); // false

// Simple array
$json = Data::jsonEncode(['item1', 'item2', 'item3']);
// Result: ["item1","item2","item3"]

// Associative array
$json = Data::jsonEncode(['key' => 'value']);
// Result: {"key":"value"}

// Nested array
$json = Data::jsonEncode(['user' => ['name' => 'John', 'age' => 30]]);
// Result: {"user":{"name":"John","age":30}}

// Simple array
$array = Data::jsonDecode('["item1","item2","item3"]');
// Result: ['item1', 'item2', 'item3']

// Associative array
$array = Data::jsonDecode('{"key":"value"}');
// Result: ['key' => 'value']

// Nested array
$array = Data::jsonDecode('{"user":{"name":"John","age":30}}');
// Result: ['user' => ['name' => 'John', 'age' => 30]]

// Single value
$array = Data::valueToArray('singleValue');
// Result: ['singleValue']

// Comma-separated values
$array = Data::valueToArray('value1, value2, value3');
// Result: ['value1', 'value2', 'value3']

// Array input (returns as-is)
$array = Data::valueToArray(['arrayValue1', 'arrayValue2']);
// Result: ['arrayValue1', 'arrayValue2']

$extension = File::fileExtension('document.txt'); // Returns: "txt"
$extension = File::fileExtension('archive.tar.gz'); // Returns: "tar.gz"
$extension = File::fileExtension('ftp://server.tech/archive.tar.bz2'); // Returns: "tar.bz2"

$name = File::fileName('document.txt'); // Returns: "document.txt"
$name = File::fileName('document.txt', false); // Returns: "document"
$name = File::fileName('ftp://server.tech/archive.tar.bz2'); // Returns: "archive.tar.bz2"
$name = File::fileName('ftp://server.tech/archive.tar.bz2', false); // Returns: "archive"

$isAbsolute = File::isAbsolutePath('/var/www/html'); // true (Unix)
$isAbsolute = File::isAbsolutePath('C:\\Program Files\\App'); // true (Windows)
$isAbsolute = File::isAbsolutePath('relative/path/to/file'); // false

// Create a directory (will create all parent directories if needed)
File::ensureDirectory('/var/www/html/myapp/cache');

// Remove a directory and all its contents
File::removeDirectory('/var/www/html/myapp/cache');

$sanitized = File::sanitizeFilename(' !! 🤬 invalid ¹²% _файлнаме??.txt ');
// Returns: "!!  invalid ¹²% _файлнаме__.txt"

$sanitized = File::sanitizeFilename('aux');
// Returns: "aux_"

$isHtml = Html::isHTML('This is a simple text.'); // false
$isHtml = Html::isHTML('<p>This is a simple paragraph.</p>'); // true
$isHtml = Html::isHTML('This is a <simple> text & more.'); // true

// Simple text
$secured = Html::secure('This is a simple text.');
// Result: "This is a simple text."

// Text with HTML-like entities
$secured = Html::secure('This is a "simple" text with entities & smth else™.');
// Result: "This is a "simple" text with entities & smth else&trade;."

// HTML with purification
$secured = Html::secure('<p>This is a <b>simple</a> paragraph.</p><img src="javascript:evil();" onload="evil();" />');
// Result: "<p>This is a <b>simple paragraph.</b></p>"

// Simple text
$tidied = Html::tidy('This is a simple text.');
// Result: "This is a simple text."

// HTML with correct structure
$tidied = Html::tidy('<p>This is a <b>simple</b> paragraph.</p>');
// Result: "<p>This is a <strong>simple</strong> paragraph.</p>\n"

// HTML with incorrect structure
$tidied = Html::tidy('<p class="aaa" class="bbb">This is a <b>simple</a> paragraph.</p>');
// Result: "<p class=\"aaa bbb\">This is a <strong>simple paragraph.</strong></p>\n"

// Simple text without line breaks
$html = Html::textToHtml('This is a simple text without line breaks.');
// Result: "<p>This is a simple text without line breaks.</p>\n"

// Text with line breaks
$html = Html::textToHtml("This is a simple text\nwith single line breaks.");
// Result: "<p>This is a simple text<br />\nwith single line breaks.</p>\n"

// Multiple paragraphs
$html = Html::textToHtml("This is the first paragraph.\n\nThis is the second paragraph, with a line break.\nSee?\n\nThis is the third paragraph.");
// Result: "<p>This is the first paragraph.</p>\n<p>This is the second paragraph, with a line break.<br />\nSee?</p>\n<p>This is the third paragraph.</p>\n"

// Simple text
$tags = Html::textTags('This is a simple text.');
// Result: []

// Simple HTML
$tags = Html::textTags('<p>This is a <b>simple</b> paragraph.</p>');
// Result: ['p', 'b']

// Complex HTML
$tags = Html::textTags('<div><h1>Title</h1><p>Paragraph with <a href="#">link</a> and <span style="color:red;">styled text</span>.<img src="image.jpg" /></p></div>');
// Result: ['div', 'h1', 'p', 'a', 'span', 'img']

$zeroPadded = Text::setLeadingZeroes(5); // Returns: "05"
$zeroPadded = Text::setLeadingZeroes(5, 3); // Returns: "005"
$zeroPadded = Text::setLeadingZeroes(123, 3); // Returns: "123"
$zeroPadded = Text::setLeadingZeroes(1234, 3); // Returns: "1234"

$id = Text::classNameToId('MyClassName'); // Returns: "my-class-name"
$id = Text::classNameToId('App\\Models\\MyClassName'); // Returns: "my-class-name"
$id = Text::classNameToId('ABC'); // Returns: "a-b-c"
$id = Text::classNameToId('already-id'); // Returns: "already-id"

// Basic usage
$fixed = Text::fixSpaces('This is a test.'); // Returns: "This is a test."

// With NBSPs
$fixed = Text::fixSpaces("This is a test."); // Returns: "This is a test."
$fixed = Text::fixSpaces("This \xC2\xA0 is \xC2\xA0 a \xC2\xA0 test."); // Returns: "This is a test."

// With narrow NBSPs
$fixed = Text::fixSpaces("This \xE2\x80\xAF is \xE2\x80\xAF a \xE2\x80\xAF test."); // Returns: "This is a test."

// With RLM characters
$fixed = Text::fixSpaces("This \xE2\x80\x8F is \xE2\x80\x8F\xe2\x80\x8a a \xE2\x80\x8F test.\xe2\x80\x83"); // Returns: "This is a test."