sanitize.php
Table of Contents
Functions
- sanitize() : string|null
- Sanitize a string based on configurable flags.
Functions
sanitize()
Sanitize a string based on configurable flags.
sanitize(string|null $source[, int $flags = SanitizeFlag::DEFAULT ][, array<string|int, mixed> $options = [] ]) : string|null
This function acts as a comprehensive filter chain for string data. It can perform operations ranging from simple trimming to complex HTML stripping, Unicode normalization, and invisible character removal.
Available flags (SanitizeFlag)
Cleaning & Security:
SanitizeFlag::STRIP_TAGS: Remove HTML/PHP tags (and content of <script>/<style>).SanitizeFlag::DECODE_ENTITIES: Decode HTML entities (e.g.,&->&).SanitizeFlag::REMOVE_CONTROL_CHARS: Remove non-printable ASCII characters (0-31, 127) except line breaks/tabs.SanitizeFlag::REMOVE_INVISIBLE: Remove invisible Unicode characters (zero-width, BOM, etc.) and normalize non-breaking spaces. Formatting & Normalization:SanitizeFlag::NORMALIZE_UNICODE: Normalize string to Unicode Normalization Form C (NFC) by default.SanitizeFlag::NORMALIZE_LINE_BREAKS: Convert Windows (\r\n) and Mac (\r) line endings to Unix (\n).SanitizeFlag::REMOVE_EXTRA_LINE_BREAKS: Collapse multiple consecutive line breaks into a single one.SanitizeFlag::COLLAPSE_SPACES: Collapse multiple consecutive horizontal spaces into a single space.SanitizeFlag::TRIM: Remove whitespace from the start and end of the string. Output Control:SanitizeFlag::NULLIFY: Returnnullif the resulting string is empty.
Processing order
Operations are applied in this specific order to ensure data integrity:
- DECODE_ENTITIES: Decode HTML entities first (to expose hidden tags or chars).
- STRIP_TAGS: Remove scripts/styles content, then strip tags.
- REMOVE_CONTROL_CHARS: Clean basic ASCII control noise.
- REMOVE_INVISIBLE: aggressive cleaning of Unicode invisible chars.
- NORMALIZE_UNICODE: Standardize Unicode composition.
- NORMALIZE_LINE_BREAKS: Standardize line endings to
\n. - REMOVE_EXTRA_LINE_BREAKS: Collapse vertical spacing.
- COLLAPSE_SPACES: Collapse horizontal spacing.
- TRIM: Clean edges.
- NULLIFY: Final check for emptiness.
Parameters
- $source : string|null
-
The string to sanitize. Can be null.
- $flags : int = SanitizeFlag::DEFAULT
-
A bitmask of SanitizeFlag constants. Defaults to
SanitizeFlag::DEFAULT. - $options : array<string|int, mixed> = []
-
Optional parameters for specific flags:
allowed_tags(string|string[]): Used withSTRIP_TAGS. allowable tags (e.g. 'unicode_form(int): Used withNORMALIZE_UNICODE. Defaults toNormalizer::NFC.
Tags
Return values
string|null —The sanitized string, or null if NULLIFY is enabled and the result is empty.