Data Cleaner
True\DataCleaner is a static utility class for sanitizing user input and formatting common data — emails, phone numbers, postal codes, currency, names, URLs, and so on. Reach for it before storing form input or echoing it back to the page.
Basic usage
Every method is static — call them directly, no instance required:
use True\DataCleaner;
$clean = DataCleaner::alphaInt($input);
Stripping by character class
| Method | Keeps |
|---|---|
int($str) | Digits only. |
alpha($str) | Letters only. |
alphaInt($str) | Letters and digits. |
decimal($str) | Digits, dot, and minus sign. |
float($str) | Floating-point number with fractions. |
name($str) | Letters, digits, spaces, name punctuation. |
streetAddress($str) | Address-safe characters. |
filePath($str) | Letters, digits, hyphens. |
dbField($str) | Letters, digits, hyphens, underscores, spaces. |
postalCode($str) | Letters, digits, hyphens, spaces. |
creditCard($str) | Digits only. |
ip($str) | Validates and returns an IP address. |
Formatting
| Method | Behavior |
|---|---|
phoneFormat($ph, $type = 1, $noCountryCode = false) |
Type 1: 555-555-5555Type 2: (555) 555-5555Type 3: E.164 +15555555555
|
currency($str) | Formats a number as currency. |
postalCodeFormat($str, $country = 'US') |
US 12345 / 12345-6789, CA A1A 1A1, AU 1234. |
addDashes($cc, $type) | Adds dashes to a credit-card number per brand. |
titleCase($str) | Title-cases with sensible word exceptions. |
splitName($name) | Returns [firstName, lastName]. |
convertNum($num) | Number-to-words (English). |
HTML and meta output
| Method | Purpose |
|---|---|
email($str) | Sanitizes an email address. |
url($str) | Sanitizes a URL. |
filterOutURLs($str) | Strips URLs out of free-form text. |
forMetaTags($str) | Prepares text for <meta> content. |
encodeQuotes($str) | HTML-entity-encodes single and double quotes. |
forHtmlEditors($str) | Encodes ampersands for safe round-tripping through editors. |
htmlOutput($str) | Encodes special characters for HTML output. |
sanitize($str, $type = 1) |
Type 1: strip HTML. Type 2: strip HTML + entity-encode. Type 3: same as Type 2. |
escape($str) | Encodes special HTML characters. |
charset_decode_utf_8($str) | Decode UTF-8 to the original charset. |
Example
use True\DataCleaner;
$inputEmail = 'user@example.com';
$cleanEmail = DataCleaner::email($inputEmail);
$inputPhone = '(555) 123-4567';
$prettyPhone = DataCleaner::phoneFormat($inputPhone, 2);
$inputAddress = '123 Main St., Apt #4B';
$cleanAddress = DataCleaner::streetAddress($inputAddress);
Notes
- Methods return an empty string when the input is
null. - Inputs are assumed to be strings or
null— pass anything else at your own risk. intOnlyandalphaOnlyare deprecated. Useintandalpha.