Overview
Zero-dependency input sanitization utilities for HolyPHP.
bundled — works offline
zero dependencies
xss
escape
Quick start
import "sanitize";
$safe_html = sanitize_html("<script>alert('xss')</script>");
// "<script>alert('xss')</script>"
$plain = sanitize_strip_tags("<p>Hello <b>world</b>!</p>");
// "Hello world!"
$file = sanitize_filename("../../etc/passwd");
// "passwd"
$path = sanitize_path("uploads/2026/../images/photo.png");
// "uploads/images/photo.png"
$col = sanitize_sql_ident("user_email");
// "user_email"
$slug = sanitize_alnum("Hello, World! 123", "-");
// "HelloWorld123"
$text = sanitize_whitespace(" hello \t\n world ");
// "hello world"
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install sanitize
This adds the library to hphp.yaml, installs it into .hphp/packages/sanitize/ and pins the exact version in hphp.lock:
require:
sanitize: "^1.0"
Use it
import "sanitize";
sanitize is bundled with the compiler, so import "sanitize"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install sanitize # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install sanitize
Documentation
API Reference
sanitize_html(string $input): string
Escapes HTML special characters to prevent XSS:
& -> &
< -> <
> -> >
" -> "
' -> '
sanitize_strip_tags(string $input): string
Removes HTML/XML tags from $input, respecting quoted attribute values containing >.
sanitize_filename(string $filename): string
Sanitizes a filename by:
- Rejecting empty strings and null bytes
- Extracting the basename (stripping directory components and
\// separators)
- Replacing unsafe characters outside
[A-Za-z0-9._-] with _
- Stripping leading dots and collapsing
.. sequences
Throws Exception if the filename is empty, contains a null byte, or has no valid characters.
sanitize_path(string $path): string
Normalizes path separators (\ -> /), collapses repeated slashes, resolves . and .. segments, and rejects any .. traversal that escapes above the root.
Throws Exception if the path is empty, contains a null byte, or attempts to escape above root.
sanitize_sql_ident(string $ident): string
Validates a SQL identifier (table or column name). Must be non-empty, start with [A-Za-z_], and contain only [A-Za-z0-9_].
Throws Exception if the identifier is empty or contains invalid characters.
sanitize_alnum(string $input, string $extra = ""): string
Strips all characters except ASCII alphanumerics (A-Z, a-z, 0-9) and any characters explicitly included in $extra.
sanitize_whitespace(string $input): string
Trims leading and trailing whitespace and collapses all internal whitespace runs (spaces, tabs, newlines, carriage returns) into a single space.
Error Handling
All validation errors throw an Exception prefixed with sanitize: :
sanitize: filename cannot be empty
sanitize: filename contains null byte
sanitize: filename is invalid or contains path traversal
sanitize: filename contains no valid characters
sanitize: path cannot be empty
sanitize: path contains null byte
sanitize: path traversal detected (escapes root)
sanitize: SQL identifier cannot be empty
sanitize: SQL identifier must start with a letter or underscore
sanitize: SQL identifier contains invalid character '<ch>'
Examples
01Quick start
Bundled with the compiler — import "sanitize"; works offline in any file, or add it to a project with hphp install sanitize.
import "sanitize";
$safe_html = sanitize_html("<script>alert('xss')</script>");
// "<script>alert('xss')</script>"
$plain = sanitize_strip_tags("<p>Hello <b>world</b>!</p>");
// "Hello world!"
$file = sanitize_filename("../../etc/passwd");
// "passwd"
$path = sanitize_path("uploads/2026/../images/photo.png");
// "uploads/images/photo.png"
$col = sanitize_sql_ident("user_email");
// "user_email"
$slug = sanitize_alnum("Hello, World! 123", "-");
// "HelloWorld123"
$text = sanitize_whitespace(" hello \t\n world ");
// "hello world"
Try ithphp install sanitize
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_sanitize.hphp.