PHPackages                             laraplus/string - 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. laraplus/string

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

laraplus/string
===============

A nice fluid interface for string manipulation.

1.0.3(5y ago)45.6k3MITPHPPHP &gt;=5.5.0CI failing

Since Nov 13Pushed 1y ago2 watchersCompare

[ Source](https://github.com/laraplus/string)[ Packagist](https://packagist.org/packages/laraplus/string)[ RSS](/packages/laraplus-string/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

Laraplus/String
===============

[](#laraplusstring)

This package provides a fluent easy-to-use interface for string manipulation. It integrates with Illuminate\\Support to enable a fluent syntax even when dealing with multiple results. All methods are UTF-8 friendly.

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

[](#installation)

To install this package, simply require it using composer:

```
composer require laraplus/string

```

Example usage
-------------

[](#example-usage)

With this package you can avoid ugly nested str\_\* functions and never ending needle-haystack problems:

```
str(' some text ')->trim()->substring(0, 4);

// instead of

substr(trim(' some text '), 0, 4);
```

If you have a slug "my-blog-title-4" and you need to find the index "4", you can simply write:

```
$lastIndex = str($lastSlug)->explode('-')->last();

// instead of

$parts = $explode('-');
$lastIndex = array_pop($parts);
```

Lets say you have some custom "colon separated" multi line string that you need to parse:

```
str($content)->lines()->each(function ($row) {
    $data = str($row)->explode(':');
    // do something with $data here
});

// instead of

$lines = preg_split('/\r\n|\n|\r/', $content);
foreach($lines as $line) {
    $data = explode(':', $line);
    // do something with $data here
}
```

Full reference
--------------

[](#full-reference)

### Initialization

[](#initialization)

You can initialize the `StringBuffer` object directly or by using the `str` helper function:

```
$string = new Laraplus\String\StringBuffer('Hello world!');

// or

$string = str('Hello world!');
```

### Chaining

[](#chaining)

When the result of a method call is a string, it is automatically wrapped in a new StringBuffer instance, so that the method calls can be chained.

```
// result: "HELLO"
str('hello world')->toUpper()->wordAt(0);
```

If you need to unwrap the object, you can do that by calling the `get()` method or simply cast it to string.

```
// a string "Hello world" is produced in all examples below
str('hello world')->ucfirst()->get();
(string)str('hello world')->ucfirst();
str('hello')->ucfirst() . ' world!';
```

When a method returns an array it is automatically wrapped in a Collection object, which enables further chaining:

```
str('hello world')->words()->each(function($word){
 // Be careful, $word is just a plain string here.
 // To convert it to StringBuffer again just call: str($word)
});
```

For a full reference of available collection methods, see the Laravel documentation:

### Available methods

[](#available-methods)

#### ucfirst()

[](#ucfirst)

Capitalize the first letter of the string.

```
// result: "Hello world"
str('hello world')->ucfirst();
```

#### lcfirst()

[](#lcfirst)

Lowercase the first letter of the string.

```
// result: "hello world"
str('Hello world')->lcfirst();
```

#### startsWith($needles)

[](#startswithneedles)

Determine if the string starts with a given substring.

```
// returns true
str('Hello world')->startsWith('Hello');

// returns false
str('Hello world')->startsWith('world');

// returns true
str('Hello world')->startsWith(['H', 'W']);
```

#### endsWith($needles)

[](#endswithneedles)

Determine if the string ends with a given substring.

```
// returns true
str('Hello world')->endsWith('world');

// returns false
str('Hello world')->endsWith('Hello');

// returns true
str('Hello world')->endsWith(['o', 'd']);
```

#### contains($needles)

[](#containsneedles)

Determine if the string contains a given substring.

```
// returns true
str('Hello world')->contains('world');

// returns false
str('Hello world')->contains('universe');

// returns true
str('Hello world')->contains(['w', 'u']);
```

#### equals($needles)

[](#equalsneedles)

Determine if the string equals the given input in a constant time comparison.

```
// returns true
str('Hello world')->equals('Hello world');

// returns false
str('Hello world')->equals('Hello universe');
```

#### matches($pattern)

[](#matchespattern)

Determine if the string matches a given pattern.

```
// returns true
str('Hello/World')->matches('*/*');

// returns true
str('Hello world')->equals('Hello*');
```

#### explode($delimiters)

[](#explodedelimiters)

Split the string with given delimiter(s).

```
// result: ['Hello', ' World']
str('Hello, World')->explode(',');

// result: ['one', 'two', 'three', 'four']
str('one:two,three:four')->explode([':', ',']);
```

#### indexOf($needle, $offset = 0)

[](#indexofneedle-offset--0)

Find the first occurrence of a given needle in the string, starting at the provided offset.

```
// returns 0
str('one, two')->indexOf('o');

// returns 7
str('one, two')->indexOf('o', 1);
```

#### lastIndexOf($needle, $offset = 0)

[](#lastindexofneedle-offset--0)

Find the last occurrence of a given needle in the string, starting at the provided offset.

```
// returns 7
str('one, two')->lastIndexOf('o');

// returns 0
str('one, two')->lastIndexOf('o', 1);
```

#### replace($search, $replace, &amp;$count = 0)

[](#replacesearch-replace-count--0)

Replace all occurrences of the search string with the replacement string.

```
// result: 'one; two; three'
str('one, two, three')->replace(',', ';');

// result: 'one; two; three' and $count in incremented by 2
str('one, two, three')->replace(',', ';', $count);

// result: 'one; two; three'
str('one, two. three')->replace([',', '.'], ';');

// result: 'one; two, three'
str('one, two. three')->replace([',', '.'], [';', ',']);
```

#### substring($start, $length = null)

[](#substringstart-length--null)

Returns the portion of string specified by the start and length parameters

```
// result: 'world'
str('Hello world')->substring(6);

// result: 'll'
str('Hello world')->substring(2, 2);
```

#### toAscii()

[](#toascii)

Transliterate a UTF-8 value to ASCII.

```
// result: 'CcZzSs'
str('ČčŽžŠš')->toAscii();
```

#### toCamel()

[](#tocamel)

Convert a value to camel case.

```
// result: 'helloWorld'
str('hello_world')->toCamel();
```

#### toSnake()

[](#tosnake)

Convert a value to snake case.

```
// result: 'hello_world'
str('HelloWorld')->toSnake();
```

#### toStudly()

[](#tostudly)

Convert a value to studly case.

```
// result: 'HelloWorld'
str('hello_world')->toStudly();
```

#### toTitle()

[](#totitle)

Convert a value to title case.

```
// result: 'Hello World'
str('hello world')->toTitle();
```

#### toSlug()

[](#toslug)

Convert a value to title case.

```
// result: 'hello-world'
str('Hello world')->toSlug();
```

#### toUpper()

[](#toupper)

Convert the given string to upper-case.

```
// result: 'HELLO WORLD'
str('Hello world')->toUpper();
```

#### toLower()

[](#tolower)

Convert the given string to lower-case.

```
// result: 'hello world'
str('Hello World')->toLower();
```

#### toSingular()

[](#tosingular)

Get the singular form of an English word.

```
// result: 'person'
str('people')->toSingular();
```

#### toPlural()

[](#toplural)

Get the plural form of an English word.

```
// result: 'people'
str('person')->toPlural();
```

#### length()

[](#length)

Return the length of the given string.

```
// returns 11
str('Hello world')->length();
```

#### words($ignore = '?!;:,.')

[](#wordsignore--)

Return a Collection of individual words in the string ignoring the given characters.

```
// result: ['one', 'two', 'three']
str('one, two, three')->words();

// result: ['one', 'two', 'three']
str('(one) : (two) : (three)')->words('(:)');
```

#### lines()

[](#lines)

Return a collection of individual lines in the string.

```
// result: ['one', 'two', 'three']
str("one\ntwo\r\nthree")->lines();
```

#### prepend($string)

[](#prependstring)

Prepend a given input to the string.

```
// result: 'hello world'
str('world')->prepend(' ')->prepend('hello');
```

#### append($string)

[](#appendstring)

Append a given input to the string.

```
// result: 'hello world'
str('hello')->append(' ')->append('world');
```

#### trim($chars = null)

[](#trimchars--null)

Trim given characters from both ends of the string. If no characters are provided, all white space is trimmed.

```
// result: 'hello world'
str('  hello world  ')->trim();

// result: 'hello world'
str('--hello world--')->trim('-');
```

#### ltrim($chars = null)

[](#ltrimchars--null)

Similar to `trim()`, but only trims characters from the left side.

```
// result: 'hello world  '
str('  hello world  ')->ltrim();

// result: 'hello world--'
str('--hello world--')->ltrim('-');
```

#### rtrim($chars = null)

[](#rtrimchars--null)

Similar to `trim()`, but only trims characters from the right side.

```
// result: '  hello world'
str('  hello world  ')->rtrim();

// result: '--hello world'
str('--hello world--')->rtrim('-');
```

#### limit($limit = 100, $end = '...')

[](#limitlimit--100-end--)

Limit the number of characters in the string.

```
// result: 'hello...'
str('hello world')->limit(5);

// result: 'hello etc.'
str('hello world')->limit(5, ' etc.');
```

#### limitWords($limit = 100, $end = '...')

[](#limitwordslimit--100-end--)

Limit the number of words in the string.

```
// result: 'hello the world...'
str('Hello the world of PHP!')->limitWords(3);

// result: 'hello the world etc.'
str('Hello the world of PHP!')->limitWords(3, ' etc.');
```

#### wordAt($index)

[](#wordatindex)

Return the word at the given index.

```
// result: 'world'
str('Hello the world of PHP!')->wordAt(2);
```

#### tree($open = '{', $close = '}')

[](#treeopen---close--)

Parse a tree structure defined by the given delimiters.

```
//result: ['one', ['two', ['three'], 'four']]
str('one{two{three}four}')->tree();
```

### Using offsets

[](#using-offsets)

Since the StringBuffer class implements the ArrayAccess interface, you can also use all of the usual offset goodies:

```
$string = str('hello world');
$string[0]; // returns 'h'
isset($string[10]) // returns true
unset($string[0]); // $string becomes 'ello world'
$string[0] = 'He'; // $string becomes 'Hello world'
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 93.3% 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 ~606 days

Total

4

Last Release

2020d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/977c1e237621d1621bd391062fdde36c940d7f58b26c635e94d34d2fee68ebc5?d=identicon)[acasar](/maintainers/acasar)

---

Top Contributors

[![acasar](https://avatars.githubusercontent.com/u/6329543?v=4)](https://github.com/acasar "acasar (14 commits)")[![BlazOrazem](https://avatars.githubusercontent.com/u/5699173?v=4)](https://github.com/BlazOrazem "BlazOrazem (1 commits)")

---

Tags

laravelstring

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/laraplus-string/health.svg)

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

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[madewithlove/laravel-nova-uuid-support

Adds uuid and other string identifier support to Laravel Nova

28132.9k](/packages/madewithlove-laravel-nova-uuid-support)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[pishran/laravel-persian-string

Convert Arabic/English/etc numbers and characters to Persian numbers and characters in Laravel models.

206.7k](/packages/pishran-laravel-persian-string)[nedwors/navigator

A Laravel package to ease defining navigation menus

433.1k](/packages/nedwors-navigator)[dcblogdev/laravel-junie

Install pre-configured guides for Jetbrains Junie

392.5k](/packages/dcblogdev-laravel-junie)

PHPackages © 2026

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