PHPackages                             leafy-tech/str - 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. leafy-tech/str

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

leafy-tech/str
==============

This library offers more object-oriented and more fluent string manipulation.

v1.0.5(4y ago)027MITPHPPHP &gt;=5.6

Since Nov 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/leafy-tech/str)[ Packagist](https://packagist.org/packages/leafy-tech/str)[ RSS](/packages/leafy-tech-str/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (6)DependenciesVersions (7)Used By (0)

Fluent String
-------------

[](#fluent-string)

This library offers more object-oriented and more fluent string manipulation.

[![Maintainer](https://camo.githubusercontent.com/6c079128cc7507e8990023495418fd75b50b8114343968f141c541ddeccde47a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d456c69656c25323046657272656972612d696e666f726d6174696f6e616c)](https://camo.githubusercontent.com/6c079128cc7507e8990023495418fd75b50b8114343968f141c541ddeccde47a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d61696e7461696e65722d456c69656c25323046657272656972612d696e666f726d6174696f6e616c)[![PHP](https://camo.githubusercontent.com/66af5e5db65dfdfcd46b17e713e083c49a1e6cc7cc089db083d3bd7767a2eb59/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d2533453d352e362d626c756576696f6c6574)](https://camo.githubusercontent.com/66af5e5db65dfdfcd46b17e713e083c49a1e6cc7cc089db083d3bd7767a2eb59/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d2533453d352e362d626c756576696f6c6574)[![VERSION](https://camo.githubusercontent.com/426a21cb246c3a14e904fa8be976492f0664dee8ae79d0b79a5ae3ce202bf4e9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737461626c652d76312e302e352d626c7565)](https://camo.githubusercontent.com/426a21cb246c3a14e904fa8be976492f0664dee8ae79d0b79a5ae3ce202bf4e9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737461626c652d76312e302e352d626c7565)[![BUILD](https://camo.githubusercontent.com/2760f74cc5c4c9ce271d69f10f445c8d12b697a6e61cdd2856c6c39fb1caa624/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d706173732d73756363657373)](https://camo.githubusercontent.com/2760f74cc5c4c9ce271d69f10f445c8d12b697a6e61cdd2856c6c39fb1caa624/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d706173732d73756363657373)[![LICENSE](https://camo.githubusercontent.com/1fa72592562e90c36ed2785fc012af41a291c039166aa479a4efebe485ab7640/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d73756363657373)](https://camo.githubusercontent.com/1fa72592562e90c36ed2785fc012af41a291c039166aa479a4efebe485ab7640/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d73756363657373)

### Installation

[](#installation)

Available via Composer

`composer require leafy-tech/str`

### Usage

[](#usage)

**Str::after()**

```
   // The method returns everything after the given value in a string.
   // The entire string will be returned if the value does not exist within the string:

    use LeafyTech\Support\Str;

    $slice = Str::after('This is my name', 'This is');

    // ' my name'
```

**Str::afterLast()**

```
   // The method returns everything after the last occurrence of the given value in a string.
   // The entire string will be returned if the value does not exist within the string:

    use LeafyTech\Support\Str;

    $slice = Str::afterLast('App\Controllers\Controller', '\\');

    // 'Controller'
```

**Str::before()**

```
   // The method returns everything before the given value in a string:

    use LeafyTech\Support\Str;

    $slice = Str::before('This is my name', 'my name');

    // This is '
```

**Str::beforeLast()**

```
   // The method returns everything before the given value in a string:

    use LeafyTech\Support\Str;

    $slice = Str::beforeLast('This is my name', 'is');

    // This '
```

**Str::between()**

```
   // The method returns the portion of a string between two values:

    use LeafyTech\Support\Str;

    $slice = Str::between('This is my name', 'This', 'name');

    // is my '
```

**Str::camel()**

```
   // The method converts the given string to camelCase:

    use LeafyTech\Support\Str;

    $slice = Str::camel('foo_bar');

    // fooBar
```

**Str::contains()**

```
   // The method determines if the given string contains the given value. This method is case sensitive:

    use LeafyTech\Support\Str;

    $contains = Str::contains('This is my name', 'my');

    // true

    $contains = Str::contains('This is my name', ['my', 'foo']);

    // true
```

**Str::containsAll()**

```
   // The method determines if the given string contains all of the values in a given array:

    use LeafyTech\Support\Str;

    $contains = Str::containsAll('This is my name', ['my','name']);

    // true
```

**Str::endsWith()**

```
   // The method determines if the given string ends with the given value:

    use LeafyTech\Support\Str;

    $result = Str::endsWith('This is my name', 'name');

    // true
```

**Str::finish()**

```
   // The method adds a single instance of the given value to a string if it does not already end with that value:

    use LeafyTech\Support\Str;

    $adjusted = Str::finish('this/string', '/');

    // this/string/

    $adjusted = Str::finish('this/string/', '/');

    // this/string/
```

**Str::headline()**

```
   // The method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word's first letter capitalized:

    use LeafyTech\Support\Str;

    $headline = Str::headline('steve_jobs');

    // Steve Jobs

    $headline = Str::headline('EmailNotificationSent');

    // Email Notification Sent
```

**Str::length()**

```
   // The method returns the length of the given string:

    use LeafyTech\Support\Str;

    $length = Str::length('My name');

    // 7
```

**Str::limit()**

```
   // The method truncates the given string to the specified length:

    use LeafyTech\Support\Str;

    $truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);

    // The quick brown fox...

    //You may pass a third argument to the method to change the string that will be appended to
    // the end of the truncated string:

    $truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');

    // The quick brown fox (...)
```

**Str::lower()**

```
   // The method converts the given string to lowercase:

    use LeafyTech\Support\Str;

    $converted = Str::lower('MY NAME');

    // my name
```

**Str::mask()**

```
   // The ethod masks a portion of a string with a repeated character, and may be used to obfuscate segments of strings such as email
   // addresses and phone numbers:

    use LeafyTech\Support\Str;

    $string = Str::mask('taylor@example.com', '*', 3);

    // tay***************

    //If needed, you provide a negative number as the third argument to the mask method,
    // which will instruct the method to begin masking at the given distance from the end of the string:

    $string = Str::mask('taylor@example.com', '*', 3, 3);

    // tay***@example.com
```

**Str::padBoth()**

```
   // The method wraps PHP's str_pad function, padding both sides of a string with another
   // string until the final string reaches a desired length:

    use LeafyTech\Support\Str;

    $padded = Str::padBoth('James', 10, '_');

    // '__James___'

    $padded = Str::padBoth('James', 10);

    // '  James   '
```

**Str::padLeft()**

```
   // The method wraps PHP's str_pad function, padding both sides of a string with another
   // string until the final string reaches a desired length:

    use LeafyTech\Support\Str;

    $padded = Str::padLeft('James', 10, '-=');

    // '-=-=James'

    $padded = Str::padLeft('James', 10);

    // '   James'
```

**Str::padRight()**

```
   // The method wraps PHP's str_pad function, padding both sides of a string with another
   // string until the final string reaches a desired length:

    use LeafyTech\Support\Str;

    $padded = Str::padRight('James', 10, '-');

    // 'James-----'

    $padded = Str::padRight('James', 10);

    // 'James    '
```

**Str::remove()**

```
   // The method removes the given value or array of values from the string:

    use LeafyTech\Support\Str;

    $string = 'Peter Piper picked a peck of pickled peppers.';

    $removed = Str::remove('e', $string);

    // Ptr Pipr pickd a pck of pickld ppprs.
```

**Str::replace()**

```
   // The method replaces a given string within the string:

    use LeafyTech\Support\Str;

    $string = 'My last name';

    $removed = Str::replace('last', 'first', $string);

    // My first name
```

**Str::replaceArray()**

```
   // The method replaces a given value in the string sequentially using an array:

    use LeafyTech\Support\Str;

    $string = 'The event will take place between ? and ?';

    $replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);

    // The event will take place between 8:30 and 9:00
```

**Str::replaceFirst()**

```
   // The  method replaces the first occurrence of a given value in a string:

    use LeafyTech\Support\Str;

    $replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');

    // a quick brown fox jumps over the lazy dog
```

**Str::replaceLast()**

```
   // The method replaces the last occurrence of a given value in a string:

    use LeafyTech\Support\Str;

    $replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');

    // the quick brown fox jumps over a lazy dog
```

**Str::slug()**

```
   // The method generates a URL friendly "slug" from the given string:

    use LeafyTech\Support\Str;

    $slug = Str::slug('My Fist name is James', '-');

    // my-first-name-is-james
```

**Str::start()**

```
   // The method adds a single instance of the given value to a string if it does not
   // already start with that value:

    use LeafyTech\Support\Str;

    $adjusted = Str::start('this/string', '/');

    // /this/string

    $adjusted = Str::start('/this/string', '/');

    // /this/string
```

**Str::startsWith()**

```
   // The method determines if the given string begins with the given value:

    use LeafyTech\Support\Str;

    $result = Str::startsWith('This is my name', 'This');

    // true
```

**Str::substr()**

```
   // The method returns the portion of string specified by the start and length parameters:

    use LeafyTech\Support\Str;

    $converted = Str::substr('This is my name', 5, 2);

    // is
```

**Str::substrCount()**

```
   // The method returns the number of occurrences of a given value in the given string:

    use LeafyTech\Support\Str;

    $count = Str::substrCount('If you like ice cream, you will like snow cones.', 'like');

    // 2
```

**Str::title()**

```
   // The method converts the given string to Title Case:

    use LeafyTech\Support\Str;

    $converted = Str::title('a nice title uses the correct case');

    // A Nice Title Uses The Correct Case
```

**Str::ucfirst()**

```
   // The method returns the given string with the first character capitalized:

    use LeafyTech\Support\Str;

    $string = Str::ucfirst('foo bar');

    // Foo bar
```

**Str::upper()**

```
   // The method converts the given string to uppercase:

    use LeafyTech\Support\Str;

    $string = Str::upper('foo bar');

    // FOO BAR
```

**Str::wordCount()**

```
   // The method returns the number of words that a string contains:

    use LeafyTech\Support\Str;

    Str::wordCount('Hello, world!');

    // 2
```

**Str::words()**

```
   // The method limits the number of words in a string. An additional string may be
   // passed to this method via its third argument to specify which string should
   // be appended to the end of the truncated string:

    use LeafyTech\Support\Str;

    $result = Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');

    // Perfectly balanced, as >>>
```

**when**

```
   // The method invokes the given closure if a given condition is true. The closure will
   // receive the fluent string instance:

    use LeafyTech\Support\Str;

    $string = Str::of('Taylor')
                ->when(true, function ($string) {
                    return $string->append(' Otwell');
                });

    // 'Taylor Otwell'
```

**whenEmpty**

```
   // The method invokes the given closure if the string is empty. If the closure returns
   // a value, that value will also be returned by the whenEmpty method. If the closure
   // does not return a value, the fluent string instance will be returned:

    use LeafyTech\Support\Str;

    $string = Str::of('')->whenEmpty(function ($string) {
        return $string->trim()->prepend('Test');
    });

    // 'Test'
```

### License

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

\*\* The inspiration and some API/code for this Laravel's Str. This extension is the port of Laravel's Str component ()

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81% 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 ~35 days

Recently: every ~44 days

Total

6

Last Release

1469d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1af6380064aee246aaface6c2b36f5773e670773e7b7f02df1de33c52dd487f4?d=identicon)[elielelie](/maintainers/elielelie)

---

Top Contributors

[![eliel-ferreira](https://avatars.githubusercontent.com/u/112508779?v=4)](https://github.com/eliel-ferreira "eliel-ferreira (17 commits)")[![eliel-elie](https://avatars.githubusercontent.com/u/78655902?v=4)](https://github.com/eliel-elie "eliel-elie (4 commits)")

---

Tags

stringfluentstr

### Embed Badge

![Health badge](/badges/leafy-tech-str/health.svg)

```
[![Health](https://phpackages.com/badges/leafy-tech-str/health.svg)](https://phpackages.com/packages/leafy-tech-str)
```

###  Alternatives

[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.1k394.3M1.5k](/packages/nette-utils)[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.0M191](/packages/danielstjules-stringy)[coduo/php-to-string

Simple library that converts PHP value into strings

27112.7M10](/packages/coduo-php-to-string)[kwn/number-to-words

Multi language standalone PHP number to words converter. Fully tested, open for extensions and new languages.

4235.0M21](/packages/kwn-number-to-words)[pragmarx/ia-str

Laravel Illuminate Agnostic Str

523.5M5](/packages/pragmarx-ia-str)[opis/string

Multibyte strings as objects

7120.9M7](/packages/opis-string)

PHPackages © 2026

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