PHP Luminova: Format Phone Numbers, Domains, and Input Strings
Format phone numbers, extract host labels, compact PHP array syntax, and validate or filter input strings by type.
Use Formatter to prepare an input value for an expected format. Format a phone number for display, extract a domain from a URL, or use sanitize() to validate or replace characters according to a specific input rule.
Usage
use Luminova\Utility\Formatter;
echo Formatter::formatPhone('2025550123'); // (202) 555-0123
echo Formatter::sanitize('abc$123', Formatter::SANITIZE_KEY, '_'); // abc_123Class Definition
- Full namespace:
Luminova\Utility\Formatter - This class is final.
Constants
Use these constants for the $type argument to sanitize().
| Constant | Value |
|---|---|
SANITIZE_INT | int |
SANITIZE_IDENTIFIER | identifier |
SANITIZE_NUMERIC | numeric |
SANITIZE_KEY | key |
SANITIZE_PASSWORD | password |
SANITIZE_USERNAME | username |
SANITIZE_EMAIL | email |
SANITIZE_URL | url |
SANITIZE_MONEY | money |
SANITIZE_DOUBLE | double |
SANITIZE_ALPHABET | alphabet |
SANITIZE_PHONE | phone |
SANITIZE_NAME | name |
SANITIZE_TIMEZONE | timezone |
SANITIZE_TIME | time |
SANITIZE_DATE | date |
SANITIZE_UUID | uuid |
SANITIZE_DEFAULT | default |
Methods
formatPhone
Remove non-digit characters, then format seven digits as xxx-xxxx, ten as (xxx) xxx-xxxx, or eleven as x-xxx-xxx-xxxx. Other lengths generally return the filtered digits.
public static function formatPhone(string $phone): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$phone | string | phone address to format |
Return:
string - Return the formatted phone number.
mainDomain
Extract the host and strip leading labels using a dot-count and label-length heuristic.
public static function mainDomain(string $url): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL or hostname to extract the main domain from. |
Return:
string|null - The extracted domain, or null if the input is empty or the host cannot be parsed.
subDomain
Read the leftmost label of a host with at least three labels. When the leftmost label is www, read the second label.
public static function subDomain(string $url): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL or hostname to extract the subdomain from. |
Return:
string|null - The selected label, an empty string for hosts with fewer than three labels, or null for empty input or an unparseable host. For example, api.dev.example.com returns api and www.example.com returns example.
sanitize
Sanitize or validate user input based on the specified type.
Choose a type-specific validation or replacement rule. Output escaping remains necessary when displaying the result.
The namespaced helpers Luminova\Funcs\sanitize() and Luminova\Funcs\strict() also use this utility.
public static function sanitize(
string $value,
string $type = self::SANITIZE_DEFAULT,
?string $replacement = ''
): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$value | string | The input string to sanitize or validate. |
$type | string | Sanitization type, such as self::SANITIZE_* (default: self::SANITIZE_DEFAULT). |
$replacement | string\|null | Replacement for disallowed characters, (default: ''). |
Return:
string|null - The processed value, or null when a validation-only type rejects the value in replacement mode or the replacement operation fails.
Throws:
- Luminova\Exceptions\InvalidArgumentException if the type is unsupported or strict validation rejects the value.
Supported types
| Type | Description |
|---|---|
int | Only numeric digits (0-9). |
numeric / digit | Numeric values including negative and decimal numbers |
identifier | A Unicode letter followed by Unicode letters, numbers, underscores, or hyphens. |
key | Alphanumeric, underscore, hyphen. |
password | Strict password pattern: uppercase, lowercase, digits, special chars. Replacement not allowed. |
username | 3–30 chars: letters, digits, underscore, hyphen, dot. Replacement not allowed. |
email | Valid email characters. Replacement not allowed. |
url | URL-safe characters. Replacement not allowed. |
money | Numeric values, optional decimal, optional negative. |
double | Floating point numbers. |
alphabet | Unicode letters only. |
phone | Digits, +, -. |
name | Unicode letters, digits, spaces, apostrophes, underscores, dots, hyphens. |
timezone | Letters, numbers, slash, underscore, colon, hyphen. |
time | Numeric and colon, e.g., HH:MM:SS. |
date | YYYY-MM-DD or YYYY-MM-DD HH:MM:SS. |
uuid | Standard UUID format 8-4-4-4-12 hex. |
default | In replacement mode, replaces matched opening-and-closing tag pairs and their contents. Strict mode rejects tag-like markup. |
Note:
Types marked as “Replacement not allowed” only support validation;
$replacementis ignored.
Examples:
Extract digits from input containing a paired HTML tag
use Luminova\Utility\Formatter;
$input = '1235hJndhb@<script>alert("hello");</script>';
Formatter::sanitize($input, 'int'); // Returns 1235Sanitize numeric input
use Luminova\Utility\Formatter;
$value = Formatter::sanitize('1234', 'int'); // 1234
$value = Formatter::sanitize('12.34', 'numeric'); // 12.34Validate only, throws exception on invalid input
use Luminova\Utility\Formatter;
Formatter::sanitize('abc123', 'int', null); // Throws InvalidArgumentExceptionReplace invalid characters
use Luminova\Utility\Formatter;
$value = Formatter::sanitize('abc$123', 'key', '_'); // abc_123Default type (remove paired tags and their contents)
use Luminova\Utility\Formatter;
$value = Formatter::sanitize('<b>Hello</b> World'); // 'World'Unsupported type
use Luminova\Utility\Formatter;
Formatter::sanitize('value', 'unknown'); // Throws InvalidArgumentExceptionNote:
- Numeric shortcuts (
int,numeric) efficiently bypass regex when input is already valid.- For types that support replacement, the non-default modes replace matched opening-and-closing HTML tag pairs and their contents before filtering characters.
- Use
$replacement = nullfor strict validation. Apply the escaping appropriate to where you render the result.
minifyArray
Serialize an array with var_export() and compact its PHP syntax. This produces PHP source text.
public static function minifyArray(array $array): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$array | array<int\|string,mixed> | Array to format. |
Return:
string - Compact PHP array expression.