https://github.com/greg0/string-builder
Simple PHP string builder inspired by C# StringBuilder
https://github.com/greg0/string-builder
builder php simple string
Last synced: about 1 year ago
JSON representation
Simple PHP string builder inspired by C# StringBuilder
- Host: GitHub
- URL: https://github.com/greg0/string-builder
- Owner: Greg0
- Created: 2018-09-20T18:53:40.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-09-20T19:11:02.000Z (over 7 years ago)
- Last Synced: 2025-02-01T14:26:35.461Z (over 1 year ago)
- Topics: builder, php, simple, string
- Language: PHP
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# String builder
[](https://travis-ci.org/Greg0/string-builder)
[](https://packagist.org/packages/greg0/string-builder#latest)

Simple PHP string builder inspired by C# [StringBuilder](https://docs.microsoft.com/pl-pl/dotnet/api/system.text.stringbuilder)
## Install
`composer require greg0/string-builder`
## Sample usages
Creating string
```php
$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:
```text
Initial string appended string
Other paragraph
Value: 23
End of poem.
```
There are provided some string manipulation methods:
#### Insert string into position:
```php
$sb = new StringBuilder('---[]---');
$sb->insert(4, 'o.o');
echo $sb->toString(); // ---[o.o]---
```
```php
$sb = new StringBuilder('---[]---');
$sb->insert(4, 'o', 2);
echo $sb->toString(); // ---[oo]---
```
#### Removes the specified range of characters
```php
$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
```php
$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->replace('ipsum', 'lirum');
echo $sb->toString(); // Lorem lirum dolor sit amet.
```
#### Clear string
```php
$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->clear();
echo $sb->toString(); // will return empty string
```
More examples provided in unit tests.
## 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)