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

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

greg0/string-builder
====================

Simple PHP string builder inspired by C# StringBuilder

0.1(7y ago)010MITPHPPHP &gt;=7.1

Since Sep 20Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Greg0/string-builder)[ Packagist](https://packagist.org/packages/greg0/string-builder)[ Docs](https://github.com/Greg0/string-builder)[ RSS](/packages/greg0-string-builder/feed)WikiDiscussions master Synced 4d ago

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

String builder
==============

[](#string-builder)

[![Build Status](https://camo.githubusercontent.com/ce4694cd56e28f63f19e4c06faf9075b1f18184df2f379295255bad7869d9106/68747470733a2f2f7472617669732d63692e6f72672f47726567302f737472696e672d6275696c6465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Greg0/string-builder)[![Current Version](https://camo.githubusercontent.com/6ad3ce418aa167864e4f672ef4860a551f2e253a2fcc8f7068a27c4cfe01f9de/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67726567302f737472696e672d6275696c6465722e737667)](https://packagist.org/packages/greg0/string-builder#latest)[![PHP Version](https://camo.githubusercontent.com/44121f13cc3f00ab0e9ec252cbd89cbf86fc9064f570d86efb519b888d576dc7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f67726567302f737472696e672d6275696c6465722e737667)](https://camo.githubusercontent.com/44121f13cc3f00ab0e9ec252cbd89cbf86fc9064f570d86efb519b888d576dc7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f67726567302f737472696e672d6275696c6465722e737667)

Simple PHP string builder inspired by C# [StringBuilder](https://docs.microsoft.com/pl-pl/dotnet/api/system.text.stringbuilder)

Install
-------

[](#install)

`composer require greg0/string-builder`

Sample usages
-------------

[](#sample-usages)

Creating string

```
$sb = new StringBuilder('Initial string');
$sb->append(' appended string');
$sb->appendLine();
$sb->appendLine('Other paragraph');
$sb->appendFormat('%s: %d', 'Value', 23);
$sb->appendLine();
$sb->append('End of poem.');

echo $sb->toString(); // echo (string)$sb;
```

Result:

```
Initial string appended string
Other paragraph
Value: 23
End of poem.

```

There are provided some string manipulation methods:

#### Insert string into position:

[](#insert-string-into-position)

```
$sb = new StringBuilder('---[]---');
$sb->insert(4, 'o.o');

echo $sb->toString(); // ---[o.o]---
```

```
$sb = new StringBuilder('---[]---');
$sb->insert(4, 'o', 2);

echo $sb->toString(); // ---[oo]---
```

#### Removes the specified range of characters

[](#removes-the-specified-range-of-characters)

```
$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->remove(6, 5); // remove "ipsum"
echo $sb->toString(); // Lorem  dolor sit amet.
```

#### Replaces all occurrences of a specified string with another specified string

[](#replaces-all-occurrences-of-a-specified-string-with-another-specified-string)

```
$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->replace('ipsum', 'lirum');
echo $sb->toString(); // Lorem lirum dolor sit amet.
```

#### Clear string

[](#clear-string)

```
$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->clear();
echo $sb->toString(); // will return empty string
```

More examples provided in unit tests.

TODO
----

[](#todo)

- Encoding support
- More test cases
- Advanced "Format" method (see [StringBuilder.AppendFormat](https://docs.microsoft.com/pl-pl/dotnet/api/system.text.stringbuilder.appendformat))
- Many different interface implementations (e.g. Streams)

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

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

2794d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a73e0229d14ee3bcf21d24b6f8edb54e9b7f05fc51399f3d5a45914fd78b447d?d=identicon)[Greg0](/maintainers/Greg0)

---

Tags

builderphpsimplestringphpstringbuilderSimple

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[coduo/php-to-string

Simple library that converts PHP value into strings

27112.7M10](/packages/coduo-php-to-string)[gherkins/regexpbuilderphp

PHP port of thebinarysearchtree/regexpbuilderjs

1.4k163.0k1](/packages/gherkins-regexpbuilderphp)[fab2s/souuid

Simple Ordered Uuid Generator in PHP

13573.2k1](/packages/fab2s-souuid)

PHPackages © 2026

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