Luminova Framework

PHP Luminova: Format Phone Numbers, Domains, and Input Strings

Last updated: 2026-11-28 20:57:12

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_123

Class Definition

  • Full namespace: Luminova\Utility\Formatter
  • This class is final.

Constants

Use these constants for the $type argument to sanitize().

ConstantValue
SANITIZE_INTint
SANITIZE_IDENTIFIERidentifier
SANITIZE_NUMERICnumeric
SANITIZE_KEYkey
SANITIZE_PASSWORDpassword
SANITIZE_USERNAMEusername
SANITIZE_EMAILemail
SANITIZE_URLurl
SANITIZE_MONEYmoney
SANITIZE_DOUBLEdouble
SANITIZE_ALPHABETalphabet
SANITIZE_PHONEphone
SANITIZE_NAMEname
SANITIZE_TIMEZONEtimezone
SANITIZE_TIMEtime
SANITIZE_DATEdate
SANITIZE_UUIDuuid
SANITIZE_DEFAULTdefault

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): string

Parameters:

ParameterTypeDescription
$phonestringphone 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): ?string

Parameters:

ParameterTypeDescription
$urlstringThe 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): ?string

Parameters:

ParameterTypeDescription
$urlstringThe 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 = ''
): ?string

Parameters:

ParameterTypeDescription
$valuestringThe input string to sanitize or validate.
$typestringSanitization type, such as self::SANITIZE_* (default: self::SANITIZE_DEFAULT).
$replacementstring\|nullReplacement 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:

Supported types

TypeDescription
intOnly numeric digits (0-9).
numeric / digitNumeric values including negative and decimal numbers
identifierA Unicode letter followed by Unicode letters, numbers, underscores, or hyphens.
keyAlphanumeric, underscore, hyphen.
passwordStrict password pattern: uppercase, lowercase, digits, special chars. Replacement not allowed.
username3–30 chars: letters, digits, underscore, hyphen, dot. Replacement not allowed.
emailValid email characters. Replacement not allowed.
urlURL-safe characters. Replacement not allowed.
moneyNumeric values, optional decimal, optional negative.
doubleFloating point numbers.
alphabetUnicode letters only.
phoneDigits, +, -.
nameUnicode letters, digits, spaces, apostrophes, underscores, dots, hyphens.
timezoneLetters, numbers, slash, underscore, colon, hyphen.
timeNumeric and colon, e.g., HH:MM:SS.
dateYYYY-MM-DD or YYYY-MM-DD HH:MM:SS.
uuidStandard UUID format 8-4-4-4-12 hex.
defaultIn 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; $replacement is 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 1235

Sanitize numeric input

use Luminova\Utility\Formatter;

$value = Formatter::sanitize('1234', 'int'); // 1234
$value = Formatter::sanitize('12.34', 'numeric'); // 12.34

Validate only, throws exception on invalid input

use Luminova\Utility\Formatter;

Formatter::sanitize('abc123', 'int', null); // Throws InvalidArgumentException

Replace invalid characters

use Luminova\Utility\Formatter;

$value = Formatter::sanitize('abc$123', 'key', '_'); // abc_123

Default 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 InvalidArgumentException

Note:

  • 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 = null for 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): string

Parameters:

ParameterTypeDescription
$arrayarray<int\|string,mixed>Array to format.

Return:

string - Compact PHP array expression.