Welcome to the SmartString docs. SmartString is a PHP string library whose output is XSS-safe by default: values HTML-encode themselves in string contexts, and chainable methods handle the everyday formatting around them.
New to SmartString? Read the first four pages in order; each builds on the one before. The rest are standalone: open whichever matches your task.
- Getting Started - Install, echo your first auto-encoded value, and the mental model: raw value for logic, encoded output for HTML.
- Encoding and HTML - How auto-encoding works, URL and JSON encoding, and the named methods that let real markup through:
nl2br(),rawHtml(),appendHtml(),wrapHtml(). - Text and Formatting - Stripping HTML, truncating, regex replacement, dates, numbers, percentages, math, and custom functions with
map(). - Conditionals and Error Checking - Fallbacks with
or(), targeted replacements, true/false checks, and theor404()/orDie()/orThrow()/orRedirect()guards.
- Common Patterns - Copy-paste recipes for everyday template tasks: dates, text previews, address blocks, report tables.
- Method Reference - Every method in one place, grouped by what it returns.
- Troubleshooting - Common error messages and behavior gotchas, with fixes.
- Performance - How our automatic encoding is at least 3x faster than calling
htmlspecialchars()yourself. - AI Reference - The complete API in one dense file, written for AI coding assistants.