PHPackages                             phlak/twine - 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. phlak/twine

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

phlak/twine
===========

String manipulation, leveled up!

5.0.0(1y ago)49457.3k↑47.4%23[1 PRs](https://github.com/PHLAK/Twine/pulls)2MITPHPPHP &gt;=8.1CI passing

Since Jul 4Pushed 2mo ago13 watchersCompare

[ Source](https://github.com/PHLAK/Twine)[ Packagist](https://packagist.org/packages/phlak/twine)[ GitHub Sponsors](https://github.com/sponsors/PHLAK)[ Fund](https://paypal.me/ChrisKankiewicz)[ RSS](/packages/phlak-twine/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (10)Dependencies (4)Versions (20)Used By (2)

 [![Twine](twine.png)](twine.png)

 String manipulation, leveled up! -- by, [Chris Kankiewicz](https://www.ChrisKankiewicz.com) ([@phlak.dev](https://bsky.app/profile/phlak.dev))

 [![Join the Community](https://camo.githubusercontent.com/073a08ec4c3c801a8e24c53184d95a6562d74582854cb46320bcd76ef48ea543/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4a6f696e5f7468652d436f6d6d756e6974792d3762313666662e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/PHLAK/Twine/discussions) [![Become a Sponsor](https://camo.githubusercontent.com/00da07edf5fbff7528a4743d85563603f9284f02680e0ab1e73652e680878548/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4265636f6d655f612d53706f6e736f722d6363343139352e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/users/PHLAK/sponsorship) [![One-time Donation](https://camo.githubusercontent.com/e9b5aa71ffdb17943c10c6d6b4a3132b66a938495331e488ecbdad1f3c078879/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d616b655f612d446f6e6174696f6e2d3030366262362e7376673f7374796c653d666f722d7468652d6261646765)](https://paypal.me/ChrisKankiewicz)
 [![Latest Stable Version](https://camo.githubusercontent.com/8a065520c97929a89874d8d17834475dc9a27ca80fa461235626e69769fd9869/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70686c616b2f7477696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/PHLAK/Twine) [![Total Downloads](https://camo.githubusercontent.com/d56ff6165c149e819e65bbcb383c82c2030f9ffb3eb409302b2c49d57a61c8e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70686c616b2f7477696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/PHLAK/Twine) [![License](https://camo.githubusercontent.com/ac32a07f4ec85c03b7f5806f18c7987fa35c98a43a27cba76e3657a6ef6a95c8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f70686c616b2f7477696e652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/PHLAK/Twine) [![Test Suite](https://camo.githubusercontent.com/f696d4ccc5fbc7512633e64b9a6f7ccc81ae5afbbc23a23ec92794ebf2d91ff7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f50484c414b2f5477696e652f746573742d73756974652e79616d6c3f7374796c653d666c61742d737175617265)](https://github.com/PHLAK/Twine/actions)

---

Introduction
------------

[](#introduction)

Twine is a string manipulation library with an expressive, fluent syntax.

Requirements
------------

[](#requirements)

- [PHP](https://php.net) &gt;= 8.1
    - The [Multibyte String](https://www.php.net/manual/en/book.mbstring.php) extension
    - The [OpenSSL](https://www.php.net/manual/en/book.openssl.php) extension

Install with Composer
---------------------

[](#install-with-composer)

```
composer require phlak/twine
```

Getting Started
---------------

[](#getting-started)

First, import Twine:

```
use PHLAK\Twine;
```

Then instantiate a Twine object by newing up a `Twine\Str` object and passing your string as the first parameter.

```
$string = new Twine\Str('john pinkerton');
```

You may also instantiate a `Twine\Str` object statically via the `make()` method.

```
$string = Twine\Str::make('john pinkerton');
```

Or use the global `str()` helper method. The method takes a string as the only parameter and returns a `Twine\Str` object.

```
$string = str('john pinkerton');
```

Once you have a concrete `Twine\Str` instance you may treat it like any other string. This includes echoing it or using any of PHP's built-in string functions against it.

```
echo $string; // Echos 'john pinkerton'

str_shuffle($string) // Returns something like 'enoipo ktnjhnr'

strlen($string); // Returns 14
```

The strength of Twine, however comes from its built-in methods.

```
$string->echo(); // Echos 'john pinkerton'
$string->shuffle(); // Returns something like 'enoipo ktnjhnr'
$string->length(); // Returns 14

// or some more interesting methods

$string->reverse(); // Returns 'notreknip nhoj'
$string->contains('pink'); // Returns true
$string->replace('pink', 'purple'); // Returns 'john purpleton'
$string->snakeCase(); // Returns 'john_pinkerton'
```

At this point you're ready to start using Twine by calling any of its many built-in methods.

Available Methods
-----------------

[](#available-methods)

[after](https://twine.phlak.net/docs/methods/after) • [append](https://twine.phlak.net/docs/methods/append) • [base64](https://twine.phlak.net/docs/methods/base64) • [base64Decode](https://twine.phlak.net/docs/methods/base64decode) • [base64Encode](https://twine.phlak.net/docs/methods/base64encode) • [bcrypt](https://twine.phlak.net/docs/methods/bcrypt) • [before](https://twine.phlak.net/docs/methods/before) • [camelCase](https://twine.phlak.net/docs/methods/camelcase) • [characters](https://twine.phlak.net/docs/methods/characters) • [chunk](https://twine.phlak.net/docs/methods/chunk) • [contains](https://twine.phlak.net/docs/methods/contains) • [count](https://twine.phlak.net/docs/methods/count) • [crc32](https://twine.phlak.net/docs/methods/crc32) • [crypt](https://twine.phlak.net/docs/methods/crypt) • [decrypt](https://twine.phlak.net/docs/methods/decrypt) • [echo](https://twine.phlak.net/docs/methods/echo) • [encoding](https://twine.phlak.net/docs/methods/encoding) • [encrypt](https://twine.phlak.net/docs/methods/encrypt) • [endsWith](https://twine.phlak.net/docs/methods/endswith) • [equals](https://twine.phlak.net/docs/methods/equals) • [explode](https://twine.phlak.net/docs/methods/explode) • [first](https://twine.phlak.net/docs/methods/first) • [format](https://twine.phlak.net/docs/methods/format) • [from](https://twine.phlak.net/docs/methods/from) • [hex](https://twine.phlak.net/docs/methods/hex) • [hexEncode](https://twine.phlak.net/docs/methods/hexencode) • [hexDecode](https://twine.phlak.net/docs/methods/hexdecode) • [insensitiveMatch](https://twine.phlak.net/docs/methods/insensitivematch) • [insert](https://twine.phlak.net/docs/methods/insert) • [in](https://twine.phlak.net/docs/methods/in) • [isAlphabetic](https://twine.phlak.net/docs/methods/isalphabetic) • [isAlphanumeric](https://twine.phlak.net/docs/methods/isalphanumeric) • [isEmpty](https://twine.phlak.net/docs/methods/isempty) • [isLowercase](https://twine.phlak.net/docs/methods/islowercase) • [isNotEmpty](https://twine.phlak.net/docs/methods/isnotempty) • [isNumeric](https://twine.phlak.net/docs/methods/isnumeric) • [isPrintable](https://twine.phlak.net/docs/methods/isprintable) • [isPunctuation](https://twine.phlak.net/docs/methods/ispunctuation) • [isUppercase](https://twine.phlak.net/docs/methods/isuppercase) • [isWhitespace](https://twine.phlak.net/docs/methods/iswhitespace) • [join](https://twine.phlak.net/docs/methods/join) • [kebabCase](https://twine.phlak.net/docs/methods/kebabcase) • [last](https://twine.phlak.net/docs/methods/last) • [length](https://twine.phlak.net/docs/methods/length) • [lowercase](https://twine.phlak.net/docs/methods/lowercase) • [lowercaseFirst](https://twine.phlak.net/docs/methods/lowercasefirst) • [lowercaseWords](https://twine.phlak.net/docs/methods/lowercasewords) • [match](https://twine.phlak.net/docs/methods/match) • [matchAll](https://twine.phlak.net/docs/methods/matchall) • [matches](https://twine.phlak.net/docs/methods/matches) • [md5](https://twine.phlak.net/docs/methods/md5) • [nth](https://twine.phlak.net/docs/methods/nth) • [pad](https://twine.phlak.net/docs/methods/pad) • [padBoth](https://twine.phlak.net/docs/methods/padboth) • [padLeft](https://twine.phlak.net/docs/methods/padleft) • [padRight](https://twine.phlak.net/docs/methods/padright) • [pascalCase](https://twine.phlak.net/docs/methods/pascalcase) • [prepend](https://twine.phlak.net/docs/methods/prepend) • [repeat](https://twine.phlak.net/docs/methods/repeat) • [replace](https://twine.phlak.net/docs/methods/replace) • [reverse](https://twine.phlak.net/docs/methods/reverse) • [sha1](https://twine.phlak.net/docs/methods/sha1) • [sha256](https://twine.phlak.net/docs/methods/sha256) • [shuffle](https://twine.phlak.net/docs/methods/shuffle) • [similarity](https://twine.phlak.net/docs/methods/similarity) • [snakeCase](https://twine.phlak.net/docs/methods/snakecase) • [split](https://twine.phlak.net/docs/methods/split) • [startsWith](https://twine.phlak.net/docs/methods/startswith) • [strip](https://twine.phlak.net/docs/methods/strip) • [studlyCase](https://twine.phlak.net/docs/methods/studlycase) • [substring](https://twine.phlak.net/docs/methods/substring) • [to](https://twine.phlak.net/docs/methods/to) • [trim](https://twine.phlak.net/docs/methods/trim) • [trimLeft](https://twine.phlak.net/docs/methods/trimleft) • [trimRight](https://twine.phlak.net/docs/methods/trimright) • [truncate](https://twine.phlak.net/docs/methods/truncate) • [uppercase](https://twine.phlak.net/docs/methods/uppercase) • [uppercaseFirst](https://twine.phlak.net/docs/methods/uppercasefirst) • [uppercaseWords](https://twine.phlak.net/docs/methods/uppercasewords) • [url](https://twine.phlak.net/docs/methods/url) • [words](https://twine.phlak.net/docs/methods/words) • [wrap](https://twine.phlak.net/docs/methods/wrap) • [wrapHard](https://twine.phlak.net/docs/methods/wraphard) • [wrapSoft](https://twine.phlak.net/docs/methods/wrapsoft)

---

Method Chaining
---------------

[](#method-chaining)

A Twine string can be manipulated fluently by chaining methods. Here are a few example chains:

Perform a substring comparison:

```
$string = new Twine\Str('john pinkerton');

$string->substring(5, 4)->equals('pink'); // Returns true
```

Encode a file in compliance with [RFC 2045](https://tools.ietf.org/html/rfc2045).

```
$string = new Twine\Str(file_get_contents('garbage.bin'));

$string->base64()->wrap(76, "\r\n", Twine\Config\Wrap::HARD);
```

Additional details available in the full documentation at .

MultiByte Strings
-----------------

[](#multibyte-strings)

Twine aims for multibyte string compatibility by relying on PHP's [Multibyte String extension](https://www.php.net/manual/en/book.mbstring.php)(mbstring) to perform string operations. For this reason, the mbstring extension is required. Multibyte strings include Unicode encodings such as UTF-8 and UCS-2.

Changelog
---------

[](#changelog)

A list of changes can be found on the [GitHub Releases](https://github.com/PHLAK/Twine/releases) page.

Troubleshooting
---------------

[](#troubleshooting)

For general help and support join our [GitHub Discussion](https://github.com/PHLAK/Twine/discussions) or reach out on [Bluesky](https://bsky.app/profile/phlak.dev).

Please report bugs to the [GitHub Issue Tracker](https://github.com/PHLAK/Twine/issues).

Copyright
---------

[](#copyright)

This project is licensed under the [MIT License](https://github.com/PHLAK/Twine/blob/master/LICENSE).

###  Health Score

60

—

FairBetter than 98% of packages

Maintenance66

Regular maintenance activity

Popularity50

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 91.5% 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 ~181 days

Recently: every ~554 days

Total

16

Last Release

564d ago

Major Versions

0.2.0 → 1.0.02017-09-11

1.0.0 → 2.0.02018-07-08

2.3.0 → 3.0.0-rc.12018-09-18

3.0.0 → 4.0.02018-11-21

4.2.0 → 5.0.02024-12-16

PHP version history (4 changes)0.1.0PHP &gt;=5.6

2.0.0PHP &gt;=7.0

4.0.1PHP &gt;=7.2

5.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/53531?v=4)[Chris Kankiewicz](/maintainers/PHLAK)[@PHLAK](https://github.com/PHLAK)

---

Top Contributors

[![PHLAK](https://avatars.githubusercontent.com/u/53531?v=4)](https://github.com/PHLAK "PHLAK (303 commits)")[![erikverbeek](https://avatars.githubusercontent.com/u/21066218?v=4)](https://github.com/erikverbeek "erikverbeek (14 commits)")[![aman95](https://avatars.githubusercontent.com/u/10391763?v=4)](https://github.com/aman95 "aman95 (3 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")[![aalwash](https://avatars.githubusercontent.com/u/3220347?v=4)](https://github.com/aalwash "aalwash (2 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")[![voku](https://avatars.githubusercontent.com/u/264695?v=4)](https://github.com/voku "voku (1 commits)")[![Vusys](https://avatars.githubusercontent.com/u/4213522?v=4)](https://github.com/Vusys "Vusys (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![einenlum](https://avatars.githubusercontent.com/u/5675200?v=4)](https://github.com/einenlum "einenlum (1 commits)")[![mtsknn](https://avatars.githubusercontent.com/u/2226144?v=4)](https://github.com/mtsknn "mtsknn (1 commits)")

---

Tags

multi-byte-stringmultibyte-stringsphpphp-librarystring-comparisonstring-manipulationstrings

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phlak-twine/health.svg)

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

PHPackages © 2026

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