PHPackages                             itools/smartstring - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. itools/smartstring

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

itools/smartstring
==================

Enhanced PHP strings with automatic HTML encoding and chainable transformation methods

v2.7.0(1mo ago)09.6k↑186.7%[1 PRs](https://github.com/interactivetools-com/SmartString/pulls)2MITPHPPHP ^8.1CI passing

Since Aug 28Pushed 3w agoCompare

[ Source](https://github.com/interactivetools-com/SmartString)[ Packagist](https://packagist.org/packages/itools/smartstring)[ Docs](https://github.com/interactivetools-com/SmartString)[ RSS](/packages/itools-smartstring/feed)WikiDiscussions main Synced 2w ago

READMEChangelogDependencies (8)Versions (32)Used By (2)

SmartString: Secure and Simple String Handling for PHP
======================================================

[](#smartstring-secure-and-simple-string-handling-for-php)

SmartString lets you write template code that's shorter, easier to read, and XSS-safe by default. The whole library is built around a single idea: the easiest way to output data should also be the safest way.

Instead of writing code like this:

```
echo "" . htmlspecialchars($article['title'], ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML5, 'UTF-8') . "";
$summary = strip_tags($article['content']);                                             // remove tags
$summary = html_entity_decode($summary, ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML5, 'UTF-8');  // decode entities
$summary = substr($summary, 0, 120);                                                    // limit to 120 characters
echo "Summary: " . htmlspecialchars($summary, ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML5, 'UTF-8') . "...";
```

You can write code like this:

```
echo "$article->title";
echo "Summary: {$article->content->textOnly()->maxChars(120, '...')}\n";
```

SmartString encodes HTML output automatically and includes utility functions for common template tasks. A few basics cover most pages, and the rest is there when you need it.

Documentation
-------------

[](#documentation)

Full guides and references ([browse on GitHub](https://github.com/interactivetools-com/SmartString)):

- **The Basics** (read in order)
    - [Getting Started](docs/getting-started.md) - install, your first auto-encoded value, and the mental model
    - [Encoding and HTML](docs/encoding-and-html.md) - how auto-encoding works and the methods that let real markup through
    - [Text and Formatting](docs/text-and-formatting.md) - truncating, regex, dates, numbers, math, and custom functions with `map()`
    - [Conditionals and Error Checking](docs/conditionals-and-error-checking.md) - fallbacks, checks, and the guards that stop the page
- **Everyday Use**
    - [Common Patterns](docs/common-patterns.md) - copy-paste recipes for everyday template tasks
- **Lookup**
    - [Method Reference](docs/method-reference.md) - every method, grouped by what it returns
    - [Troubleshooting](docs/troubleshooting.md) - common error messages and gotchas, with fixes
    - [Performance](docs/performance.md) - how our automatic encoding is 3x faster than calling `htmlspecialchars()` yourself
    - [AI Reference](docs/ai-reference.md) - the complete API in one dense file, written for AI coding assistants

You're Never Locked In
----------------------

[](#youre-never-locked-in)

SmartString is built for outputting HTML. For everything else (calculations, business logic, or anywhere you just prefer plain PHP), call `value()` and you have the original value back, in its original type:

```
// SmartString: ->value() returns the original value, in its original type
$name = $user->name->value();

// SmartArray (companion library): ->toArray() returns a plain nested array
$rows = $orders->toArray();
```

Related Libraries
-----------------

[](#related-libraries)

- [SmartArray](https://github.com/interactivetools-com/SmartArray) - arrays as collections of SmartStrings, with chainable methods for filtering, sorting, and grouping.
- [ZenDB](https://github.com/interactivetools-com/ZenDB) - database library that returns query results as SmartArrays of SmartStrings, so fields arrive HTML-safe.

Questions?
----------

[](#questions)

This library was developed for CMS Builder, post a message in our "CMS Builder" forum here:

License
-------

[](#license)

MIT

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance93

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 99.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~23 days

Recently: every ~35 days

Total

30

Last Release

42d ago

Major Versions

v1.3.2 → v2.0.02024-11-27

PHP version history (2 changes)v1.0.0PHP ^8.0

v2.2.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/498283?v=4)[itools](/maintainers/itools)[@iTools](https://github.com/iTools)

---

Top Contributors

[![daveedis](https://avatars.githubusercontent.com/u/22456364?v=4)](https://github.com/daveedis "daveedis (117 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

jsonutf8stringsecuritymanipulationencodingmultibytehtmlformatfluentxssEscapesanitizestriptrimchainableUTFmethodstruncateurlencodesmartarray

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/itools-smartstring/health.svg)

```
[![Health](https://phpackages.com/badges/itools-smartstring/health.svg)](https://phpackages.com/packages/itools-smartstring)
```

###  Alternatives

[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.4M193](/packages/danielstjules-stringy)[voku/stringy

A string manipulation library with multibyte support

1804.0M28](/packages/voku-stringy)[xemlock/htmlpurifier-html5

HTML5 support for HTML Purifier

1063.3M20](/packages/xemlock-htmlpurifier-html5)[statamic/stringy

A string manipulation library with multibyte support, forked from @statamic

245.2M19](/packages/statamic-stringy)[tcb13/substringy

A sub string manipulation library with multibyte support that extends Stringy

1761.1k1](/packages/tcb13-substringy)[hallindavid/manny

a package of manipulators that hopefully come in useful for those of us who always forget regex when we need it (manny is short for manipulation)

37110.6k3](/packages/hallindavid-manny)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
