PHP Classes

File: build/docs/

Recommend this page to a friend!
  Classes of Lars Moelleken   Stringy   build/docs/   Download  
File: build/docs/
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Stringy
Manipulate strings encoded using Unicode
Author: By
Last change:
Date: 3 years ago
Size: 7,723 bytes


Class file image Download

Build Status Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

:accept: Stringy

A PHP string manipulation library with multibyte support. Compatible with PHP 7+

100% compatible with the original "Stringy" library, but this fork is optimized for performance and is using PHP 7+ features.

s('string')->toTitleCase()->ensureRight('y') == 'Stringy'


In part due to a lack of multibyte support (including UTF-8) across many of PHP's standard string functions. But also to offer an OO wrapper around the mbstring module's multibyte-compatible functions. Stringy handles some quirks, provides additional functionality, and hopefully makes strings a little easier to work with!

// Standard library
strtoupper('fb?');       // 'FB?'
strlen('fb?');           // 10

// mbstring
mb_strtoupper('fb?');    // 'FB?'
mb_strlen('fb?');        // '6'

// Stringy
$stringy = Stringy\Stringy::create('fb?');
$stringy->toUpperCase();    // 'FB?'
$stringy->length();         // '6'


If you like a more Functional Way to edit strings, then you can take a look at voku/portable-utf8, also "voku/Stringy" used the functions from the "Portable UTF-8"-Class but in a more Object Oriented Way.

// Portable UTF-8
use voku\helper\UTF8;
UTF8::strtoupper('fb?');    // 'FB?'
UTF8::strlen('fb?');        // '6'

Installation via "composer require"

composer require voku/stringy

Installation via composer (manually)

If you're using Composer to manage dependencies, you can include the following in your composer.json file:

"require": {
    "voku/stringy": "~6.0"

Then, after running composer update or php composer.phar update, you can load the class using Composer's autoloading:

require 'vendor/autoload.php';

Otherwise, you can simply require the file directly:

require_once 'path/to/Stringy/src/Stringy.php';

And in either case, I'd suggest using an alias.

use Stringy\Stringy as S;

OO and Chaining

The library offers OO method chaining, as seen below:

use Stringy\Stringy as S;
echo S::create('f     b?')->collapseWhitespace()->swapCase(); // 'F B?'

Stringy\Stringy has a __toString() method, which returns the current string when the object is used in a string context, ie: (string) S::create('foo') // 'foo'

Implemented Interfaces

Stringy\Stringy implements the IteratorAggregate interface, meaning that foreach can be used with an instance of the class:

$stringy = S::create('fb?');
foreach ($stringy as $char) {
    echo $char;
// 'fb?'

It implements the Countable interface, enabling the use of count() to retrieve the number of characters in the string:

$stringy = S::create('f');
count($stringy);  // 3

Furthermore, the ArrayAccess interface has been implemented. As a result, isset() can be used to check if a character at a specific index exists. And since Stringy\Stringy is immutable, any call to offsetSet or offsetUnset will throw an exception. offsetGet has been implemented, however, and accepts both positive and negative indexes. Invalid indexes result in an OutOfBoundsException.

$stringy = S::create('b?');
echo $stringy[2];     // '?'
echo $stringy[-2];    // ''
isset($stringy[-4]);  // false

$stringy[3];          // OutOfBoundsException
$stringy[2] = 'a';    // Exception

PHP Class Call Creation

As of PHP 5.6+, use function is available for importing functions. Stringy exposes a namespaced function, Stringy\create, which emits the same behaviour as Stringy\Stringy::create().

use function Stringy\create as s;

// Instead of: S::create('f     b?')
s('f     b?')->collapseWhitespace()->swapCase();

Class methods

create(mixed $str [, $encoding ])

Creates a Stringy object and assigns both str and encoding properties the supplied values. $str is cast to a string prior to assignment, and if $encoding is not specified, it defaults to mb_internal_encoding(). It then returns the initialized object. Throws an InvalidArgumentException if the first argument is an array or object without a __toString method.

$stringy = S::create('fb?', 'UTF-8'); // 'fb?'

If you need a collection of Stringy objects you can use the S::collection() method.

$stringyCollection = \Stringy\collection(['fb?', 'lall', '']);

Instance Methods

Stringy objects are immutable. All examples below make use of PHP 5.6 function importing, and PHP 5.4 short array syntax. They also assume the encoding returned by mb_internal_encoding() is UTF-8. For further details, see the documentation for the create method above.




From the project directory, tests can be ran using phpunit


For support and donations please visit Github | Issues | PayPal | Patreon.

For status updates and release announcements please visit Releases | Twitter | Patreon.

For professional support please contact me.


  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
  • Thanks to StyleCI for the simple but powerfull code style check.
  • Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!


Released under the MIT License - see LICENSE.txt for details.