sanitize

Input sanitization for HTML, filenames, paths, SQL identifiers and whitespace.

v1.0.0Application & logicbundledimport "sanitize";
Overview Installation Documentation Examples

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>");
// "&lt;script&gt;alert(&#039;xss&#039;)&lt;/script&gt;"

$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
Packagesanitize
Version1.0.0 (follows the compiler)
Entry filesanitize.hphp
Sourcepackages/lib/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:

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

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>");
// "&lt;script&gt;alert(&#039;xss&#039;)&lt;/script&gt;"

$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 it
hphp install sanitize
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_sanitize.hphp.