class format

internal  
 

Utility functions for data formatting.

Methods

static string
slugify(string $s, string|null $charlist = null, bool $lower = true)

Generates a web-friendly slug from a string.

static string
add_mask_cnpj(string $cnpj)

Applies mask to Brazilian CNPJ in the format 00.000.000/0000-00.

static string
hide_mask_cpf(string $cpf)

Partially hides a Brazilian CPF keeping only the first 3 and last 2 digits.

static string
hide_mask_email(string $email)

Partially hides an email address, preserving the first 3 characters of the name and the entity TLD.

static string
format_size_readable(float|int $value)

Converts a size in bytes to a human-readable representation (B, KB, MB, GB, TB).

static int
parse_bytes(string $val)

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.

Parameters

string $s

input text

string|null $charlist

additional list of allowed characters (optional)

bool $lower

forces lowercase in the result (default: true)

Return Value

string

generated slug

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.

Parameters

string $cnpj

CNPJ with or without mask

Return Value

string

masked CNPJ or original value if invalid

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.

Parameters

string $cpf

CPF with or without mask

Return Value

string

partially masked CPF or original value if invalid

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: [email protected] -> 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 '@***'.

Parameters

string $email

email address

Return Value

string

masked email or original value if invalid

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).

Parameters

float|int $value

size in bytes

Return Value

string

formatted representation with 2 decimal places and suffix

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

Parameters

string $val

value to convert

Return Value

int

total bytes or -1 for no limit