format
class format
| internal |
Utility functions for data formatting.
Methods
Generates a web-friendly slug from a string.
Applies mask to Brazilian CNPJ in the format 00.000.000/0000-00.
Partially hides a Brazilian CPF keeping only the first 3 and last 2 digits.
Partially hides an email address, preserving the first 3 characters of the name and the entity TLD.
Converts a size in bytes to a human-readable representation (B, KB, MB, GB, TB).
Parses human-readable memory values (e.g., "512M", "1G") into bytes (only numerics).
Details
at line 41
static string
slugify(string $s, string|null $charlist = null, bool $lower = true)
Generates a web-friendly slug from a string.
- Removes/normalizes accents and special characters.
- Uses hyphen as separator.
at line 56
static string
add_mask_cnpj(string $cnpj)
Applies mask to Brazilian CNPJ in the format 00.000.000/0000-00.
If the input does not have exactly 14 digits (after removing non-numeric characters), the original string is returned unchanged.
at line 80
static string
hide_mask_cpf(string $cpf)
Partially hides a Brazilian CPF keeping only the first 3 and last 2 digits.
Example: 123.456.789-00 -> 123..-00 If the input does not have 11 digits (after cleaning), returns the original value.
at line 104
static string
hide_mask_email(string $email)
Partially hides an email address, preserving the first 3 characters of the name and the entity TLD.
Example: joaosilva@empresa.com.br -> joa@.com.br Rules:
- Invalid emails return the original input.
- Names shorter than 3 chars preserve only 1 or 2, masking the rest.
- Domains without a dot keep only '@***'.
at line 135
static string
format_size_readable(float|int $value)
Converts a size in bytes to a human-readable representation (B, KB, MB, GB, TB).
at line 165
static int
parse_bytes(string $val)
Parses human-readable memory values (e.g., "512M", "1G") into bytes (only numerics).
Rules:
- "-1" and empty string return -1 (no limit).
- Supports suffixes: B, K, M, G, T (case-insensitive).
- Values without suffix are treated as bytes.
Examples:
- parse_bytes("512M") => 536870912
- parse_bytes("1G") => 1073741824
- parse_bytes("-1") => -1