janmensik / phphelpers
Pack of small helper functions for transformations input and outputs.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/janmensik/phphelpers
README
PHPHelpers is a lightweight PHP library that provides a collection of utility functions to simplify common tasks such as string manipulation, data parsing, URL handling, and file system operations. It is designed to be easy to integrate and use in any PHP project.
Installation
Install the library via Composer:
composer require janmensik/phphelpers
Usage
Helpers Class
The Helpers class contains a variety of static methods for common tasks.
String Manipulation
utf2ascii(string $string): string
Converts a UTF-8 encoded string to its ASCII equivalent, removing diacritics. This is particularly useful for handling Latin-1 and Czech characters.
use JanMensik\PHPHelpers\Helpers; $asciiString = Helpers::utf2ascii('Příliš žluťoučký kůň'); // echo $asciiString; // "Prilis zlutoucky kun"
text2seolink(string $string): string
Transforms a string into a URL-friendly "slug" by converting it to lowercase, replacing non-alphanumeric characters with hyphens, and trimming any leading or trailing hyphens.
use JanMensik\PHPHelpers\Helpers; $seoLink = Helpers::text2seolink('A sample title with various characters!'); // echo $seoLink; // "a-sample-title-with-various-characters"
Data Parsing
parseFloat(?string $str): ?float
Parses a floating-point number from a string, correctly handling commas as decimal separators and ignoring whitespace.
use JanMensik\PHPHelpers\Helpers; $floatValue = Helpers::parseFloat('1 234,56'); // var_dump($floatValue); // float(1234.56)
parseDate(?string $data, bool $force = false): ?int
Parses a date string into a Unix timestamp. It supports various date formats and defaults to noon if no time is specified.
use JanMensik\PHPHelpers\Helpers; $timestamp = Helpers::parseDate('15. 12. 2023 14:30'); // echo $timestamp; // A Unix timestamp corresponding to the given date and time
UrlParameters Class
The UrlParameters class provides a simple and effective way to manipulate and construct URLs with query parameters.
Creating a URL
You can create a new URL instance from a string or from the current request URI.
use JanMensik\PHPHelpers\UrlParameters; // From a string $url = new UrlParameters('https://example.com/path?param1=value1'); // From the current request $currentUrl = new UrlParameters(); $currentUrl->fromCurrent();
Modifying Parameters
You can easily add, update, or remove query parameters.
$url->setParameter('param2', 'newValue'); // Adds or updates a parameter $url->setParameter('param1', false); // Removes a parameter
Generating Output
Once you have configured the URL, you can retrieve it as a string or as a complete HTML link.
$urlString = $url->getUrl(); // echo $urlString; // "https://example.com/path?param2=newValue" $htmlLink = $url->getLink('Click Here', 'class="btn"'); // echo $htmlLink; // '<a href="https://example.com/path?param2=newValue" class="btn">Click Here</a>'