Validate emails and URLs in PHP
Use filter_var to validate email addresses and http(s) URLs, with an allow-list for schemes.
Server-side PHP recipes and security-conscious helpers.
Loading snippets…
14 snippets
Use filter_var to validate email addresses and http(s) URLs, with an allow-list for schemes.
Shorten UTF-8 text with mb_ functions so accents and emoji are never cut in half.
Create URL-safe lowercase slugs from titles, removing accents and punctuation.
Re-run flaky operations with growing delays and jitter, only for chosen exception types.
Stream CSV rows as associative arrays using fgetcsv, without loading the whole file into memory.
Turn a flat list of associative arrays into groups keyed by a field or callback.
Create unpredictable tokens for password resets, API keys and CSRF protection with random_bytes.
Convert a number of bytes to KB, MB or GB with a chosen precision.
A short helper around htmlspecialchars with the right flags for HTML text and attribute values.
Protect state-changing forms with a per-session token compared in constant time.
Check a webhook or signed-request HMAC with hash_hmac and hash_equals to avoid timing attacks.
Parse JSON with exceptions on error and a depth limit, instead of silently getting null.