Symfony String

An object-oriented PHP API for strings that unifies byte, UTF-8 code point, and grapheme cluster handling

Library
Composer
vv8.1.2
1,781stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
80/100Excellent
Development Activity76
Maintenance88
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture85
Code Quality88
Innovation72
Learning Curve78

Symfony String replaces PHP’s inconsistent, procedural string functions with an object-oriented, immutable, and chainable API. It provides three interchangeable string classes — ByteString for binary-safe byte strings, CodePointString for UTF-8 code-point-aware operations, and UnicodeString for grapheme-cluster-aware operations that correctly handle combined characters and emoji — all sharing a common AbstractString interface.

Beyond core string manipulation (case conversion, trimming, padding, splitting, camelCase/snake_case conversion), the component ships a Slugger for URL-safe slug generation with locale-aware transliteration and an Inflector for singular/plural word transformation, plus LazyString for deferring expensive string computation until it’s actually needed.

What You Get

  • ByteString, CodePointString, and UnicodeString classes sharing a common AbstractString/AbstractUnicodeString API
  • Chainable, immutable string methods: trim, padStart, split, camel, snake, truncate, reverse, and more
  • A Slugger component for generating URL-safe, transliterated slugs from arbitrary Unicode text
  • An Inflector for English singular/plural word transformation
  • LazyString for deferring string computation (e.g. translation lookups) until the value is actually read

Common Use Cases

  • Generating SEO-friendly, locale-aware URL slugs from user-submitted titles
  • Correctly truncating or measuring strings containing emoji or combined Unicode characters where naive byte-length logic breaks
  • Converting between camelCase and snake_case identifiers when mapping between PHP code and database columns or APIs
  • Building translatable UI strings that defer costly formatting/lookup work via LazyString until actually rendered

Under The Hood

Architecture — The component is built around a class hierarchy rooted in AbstractString.php (726 lines), specialized by AbstractUnicodeString for Unicode-aware operations and concretized by ByteString, CodePointString, and UnicodeString.php (437 lines); each subclass overrides low-level operations (length, substring, case conversion) to operate at the correct granularity (byte, code point, or grapheme cluster) while sharing the same fluent, immutable method set. Supporting subsystems live in Slugger/ (transliteration-based slug generation) and Inflector/ (pluralization rules). Tech Stack — Requires PHP 8.4.1+ and leans on Symfony’s own polyfills (polyfill-ctype, polyfill-intl-grapheme, polyfill-intl-normalizer, polyfill-mbstring) so grapheme/Unicode handling works even without every PHP intl extension installed; symfony/emoji, symfony/intl, and symfony/translation-contracts are optional dev/runtime integrations. Code Quality — The Tests/ directory includes dedicated suites per string class (ByteStringTest, CodePointStringTest, UnicodeStringTest) plus Slugger and Inflector subfolders, run under PHPUnit; as a core Symfony component it inherits the framework’s strict BC (backward compatibility) and deprecation-annotation conventions. API Design — Every mutating-looking method (trim(), append(), slice()) returns a new immutable instance rather than mutating in place, and the three string classes expose an identical method surface, so switching between byte-, code-point-, and grapheme-aware semantics is a one-line constructor change rather than a rewrite.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search