PHPackages                             tcb13/substringy - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. tcb13/substringy

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tcb13/substringy
================

A sub string manipulation library with multibyte support that extends Stringy

1.0.0(8y ago)1761.0k↑19%21MITPHPPHP &gt;=5.4.0

Since Oct 10Pushed 8y ago1 watchersCompare

[ Source](https://github.com/TCB13/SubStringy)[ Packagist](https://packagist.org/packages/tcb13/substringy)[ Docs](https://github.com/tcb13/SubStringy)[ RSS](/packages/tcb13-substringy/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (2)Used By (1)

A PHP SubString manipulation library with multibyte support that extends Stringy. Offers OO method chaining. Tested and compatible with PHP 5.4+ and HHVM.

This library extends and adds SubString functionality to `danielstjules/Stringy` you should check it's [documentation](https://github.com/danielstjules/Stringy/blob/master/README.md) for methods inherited by SubStringy.

[![Build Status](https://camo.githubusercontent.com/a94e459d9e2b58d34d90f4cf6328b5d39a3acf7394e178e69d81677ac082cd74/68747470733a2f2f6170692e7472617669732d63692e6f72672f74636231332f537562537472696e67792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tcb13/SubStringy)

- [Installation](#installation)
- [OO and Chaining](#oo-and-chaining)
- [Use as a Trait](#use-as-a-trait)
- [Implemented Interfaces](#implemented-interfaces)
- [PHP 5.6 Creation](#php-56-creation)
- [Methods](#methods)
    - [substringAfterFirst](#substringafterfirst)
    - [substringAfterLast](#substringafterlast)
    - [substringBeforeFirst](#substringbeforefirst)
    - [substringBeforeLast](#substringbeforelast)
    - [substringBetween](#substringbetween)
    - [substringCount](#substringcount)
- [Links](#links)
- [Tests](#tests)
- [License](#license)

Installation
------------

[](#installation)

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

```
{
    "require": {
        "danielstjules/stringy": "^3.1",
        "tcb13/substringy": "^1.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/SubStringy/src/SubStringy.php';
```

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

```
use SubStringy\SubStringy as S;
```

OO and Chaining
---------------

[](#oo-and-chaining)

The library offers OO method chaining, as seen below:

```
use Stringy\Stringy as S;
echo S::create('Fòô     Bàř', 'UTF-8')->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'`

Use as a Trait
--------------

[](#use-as-a-trait)

The library also offers the possibility to be used a `trait`. With this trait you can build your own abstraction of `danielstjules/Stringy` and combine multiple extensions:

```
namespace Vendor\YourPackage;

use Stringy\Stringy;
use SubStringy\SubStringyTrait;
use SliceableStringy\SliceableStringyTrait;

class MyStringy extends Stringy
{
    use SubStringyTrait;
    use SliceableStringyTrait;
}
```

On the example bellow we can use `MyStringy` to create `Stringy` objects enhanced with the functionality of both `SubStringy` and `SliceableStringy`:

```
use YourPackage\MyStringy as S;
$sliceableSubstring = S::create('What are your plans today?')->substringAfterFirst('plans ');
echo $sliceableSubstring['4:6'];
```

Implemented Interfaces
----------------------

[](#implemented-interfaces)

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

```
$stringy = S::create('Fòô Bàř', 'UTF-8');
foreach ($stringy as $char) {
    echo $char;
}
// 'Fòô Bàř'
```

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

```
$stringy = S::create('Fòô', 'UTF-8');
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àř', 'UTF-8');
echo $stringy[2];     // 'ř'
echo $stringy[-2];    // 'à'
isset($stringy[-4]);  // false

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

PHP 5.6 Creation
----------------

[](#php-56-creation)

As of PHP 5.6, [`use function`](https://wiki.php.net/rfc/use_function) is available for importing functions. SubStringy exposes a namespaced function, `SubStringy\create`, which emits the same behaviour as `SubStringy\SubStringy::create()`. If running PHP 5.6, or another runtime that supports the `use function` syntax, you can take advantage of an even simpler API as seen below:

```
use function SubStringy\create as s;

// Instead of: S::create('Fòô     Bàř', 'UTF-8')
s('Fòô     Bàř', 'UTF-8')->collapseWhitespace()->swapCase();
```

Methods
-------

[](#methods)

All methods that return a SubStringy object or string do not modify the original. SubStringy objects are immutable.

Since this library extends and adds SubString functionality to `danielstjules/Stringy` you should check it's documentation () for methods that can also be transparently used when working with SubStringy.

*Note: If `$encoding` is not given, it defaults to `mb_internal_encoding()`.*

#### substringAfterFirst

[](#substringafterfirst)

$stringy-&gt;substringAfterFirst(string $separator)

Gets the substring after the first occurrence of a separator. If no match is found returns false.

```
S::create('What are your plans today?')->substringAfterFirst('plans ');
```

#### substringAfterLast

[](#substringafterlast)

$stringy-&gt;substringAfterLast(string $separator)

Gets the substring after the last occurrence of a separator. If no match is found returns false.

```
S::create('This is a String. How cool can a String be after all?')->substringAfterLast('String ');
```

#### substringBeforeFirst

[](#substringbeforefirst)

$stringy-&gt;substringBeforeFirst(string $separator)

Gets the substring before the first occurrence of a separator. If no match is found returns false.

```
S::create('What are your plans today?')->substringBeforeFirst(' plans');
```

#### substringBeforeLast

[](#substringbeforelast)

$stringy-&gt;substringBeforeLast(string $separator)

Gets the substring before the last occurrence of a separator. If no match is found returns false.

```
S::create('What are your plans today? Any plans for tomorrow?')->substringBeforeLast(' plans');
```

#### substringBetween

[](#substringbetween)

$stringy-&gt;substringBetween(string $start, string $end)

Extracts a substring from between two substrings present on the current string.

```
S::create('What are your plans today?')->substringBetween('your ', ' today');
```

#### substringCount

[](#substringcount)

$stringy-&gt;substringCount(string $substr)

Count the number of substring occurrences on the current string

```
S::create('how are you? are you sure you are ok?')->substringCount('are');
```

Links
-----

[](#links)

The following is a list of libraries that extend Stringy:

- [SliceableStringy](https://github.com/danielstjules/SliceableStringy): Python-like string slices in PHP

Tests
-----

[](#tests)

From the project directory, tests can be ran using `phpunit`

License
-------

[](#license)

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

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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

Unknown

Total

1

Last Release

3187d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3ce8bfb2f2f01afb988673c28641430b4214cb7ccc61c4499c9a87b973601908?d=identicon)[TCB13](/maintainers/TCB13)

---

Top Contributors

[![djmattyg007](https://avatars.githubusercontent.com/u/489338?v=4)](https://github.com/djmattyg007 "djmattyg007 (2 commits)")[![ker0x](https://avatars.githubusercontent.com/u/5331654?v=4)](https://github.com/ker0x "ker0x (2 commits)")

---

Tags

multibytephpstring-manipulationstringssubstringsutf-8stringutilityhelpersmanipulationmultibyteutilsUTFmethodssubstring

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tcb13-substringy/health.svg)

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

###  Alternatives

[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.3M192](/packages/danielstjules-stringy)[voku/stringy

A string manipulation library with multibyte support

1863.9M26](/packages/voku-stringy)[statamic/stringy

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

245.0M18](/packages/statamic-stringy)[voku/arrayy

Array manipulation library for PHP, called Arrayy!

4915.7M18](/packages/voku-arrayy)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k430.4M1.7k](/packages/nette-utils)[danielstjules/sliceable-stringy

Python string slices in PHP

4751.6k1](/packages/danielstjules-sliceable-stringy)

PHPackages © 2026

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