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

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

andydune/string-replace
=======================

Replace markers in string with data.

v1.10.1(7y ago)54671MITPHPPHP &gt;=5.6CI failing

Since Apr 27Pushed 1y ago2 watchersCompare

[ Source](https://github.com/AndyDune/StringReplace)[ Packagist](https://packagist.org/packages/andydune/string-replace)[ Docs](https://github.com/AndyDune/StringReplace)[ RSS](/packages/andydune-string-replace/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (1)Versions (18)Used By (1)

StringReplace
=============

[](#stringreplace)

[![Build Status](https://camo.githubusercontent.com/f3886193f596da5c627f60f26db954af7860cc7b2756bb5c8a990f36aabf92a6/68747470733a2f2f7472617669732d63692e6f72672f416e647944756e652f537472696e675265706c6163652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AndyDune/StringReplace)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/434c4f84cdee7f1b09fc6f2671d3528a11fe2370a44a8729b1ba3f1d0db17e3a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e647964756e652f737472696e672d7265706c6163652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/string-replace)[![Total Downloads](https://camo.githubusercontent.com/bad294441f3b9c8edfdf3b12ee032a3ad240725e808ddcc6b0c02746083c8e42/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e647964756e652f737472696e672d7265706c6163652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/string-replace)

It replace in given string meta data with real data.

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

[](#requirements)

PHP version &gt;= 7.2

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

[](#installation)

Installation using composer:

```
composer require andydune/string-replace

```

Or if composer was not installed globally:

```
php composer.phar require andydune/string-replace

```

Or edit your `composer.json`:

```
"require" : {
     "andydune/string-replace": "^1"
}

```

And execute command:

```
php composer.phar update

```

SimpleReplace
-------------

[](#simplereplace)

It's very simple and lightweight replace methods. It uses `str_replace` function.

```
use AndyDune\StringReplace\SimpleReplace;

$instance = new SimpleReplace();
$instance->one = 'one_ok';
$instance->two = 'two_ok';

$string = 'Gogoriki go #one# and #two#';
$instance->replace($string); // equals to 'Gogoriki go one_ok and two_ok'
```

There is no any logic in it and it will no replace statements if no data to replace.

PowerReplace
------------

[](#powerreplace)

It powerful replace class with string analytics with regular. There are many functions built-in lib and you may add custom easily.

### No case sensitive

[](#no-case-sensitive)

```
use AndyDune\StringReplace\PowerReplace;

$instance = new PowerReplace();
$instance->one = 'one_ok';
$instance->TWO = 'two_ok'; // upper key

$string = 'Gogoriki go #ONE# and #two#';
$instance->replace($string); // equals to 'Gogoriki go one_ok and two_ok'
```

Functions
---------

[](#functions)

Functions are described next to marker after `:` (you can change separator).

Functions can get parameters: `#CODE:maxlen(10)#` or `#CODE:maxlen("10")#`

Symbols: **:** **(** **)** **,** **"** **'** are reserved to use as parameters for function. So if you want to use it you mast encase it with quotes (or single quotes).

This is correct usage:

```
$string = "Params: #weight:prefix(\"'\"):postfix('"')#";
$string = "Params: #weight:prefix(\":\"):postfix(':')#";
$string = "Params: #weight:prefix(\"(\"):postfix(')')#";
$string = "Params: #weight:prefix(\", \"):postfix(', ')#";
```

More then one function : `#CODE:maxlen(10):escape#`

### escape

[](#escape)

Apply `htmlspecialchars` with inserted value.

```
use AndyDune\StringReplace\PowerReplace;

$string = 'Gogoriki go #ONE:escape#';
$instance = new PowerReplace();
$instance->one = 'one_ok';
$instance->replace($string);  // equals to 'Gogoriki go &lt;b&gt;one_ok&lt;/b&gt;'
```

### addcomma

[](#addcomma)

It adds comma before inserted value if it is not empty.

```
use AndyDune\StringReplace\PowerReplace;

$string = 'Gogoriki go #one##two:comma#';
$instance = new PowerReplace();
$instance->one = 'swim';
$instance->one = 'play';
$instance->replace($string);  // equals to 'Gogoriki go swim, play'

$string = 'Gogoriki go #one##two:comma#';
$instance = new PowerReplace();
$instance->one = 'swim';
$instance->replace($string);  // equals to 'Gogoriki go swim
```

`comma` function may get params: `comma(param1, param2)`

- *param1* set to `1` if you want to miss first comma appearance in string
- *param2* set to `1` if you want to begin new group of words for next missing of first comma appearance in string

```
$string = 'I know words: #it:addcomma(1)##and_it:addcomma(1)# and #and_it_2:addcomma(1, 1)#';
$instance = new PowerReplace();
$instance->setArray([
    'it' => 'eat',
    'and_it' = 'play',
    'and_it_2' = 'sleep'
    ]);
$instance->replace($string);  // equals to 'I know words: eat, play and sleep'
```

### maxlen

[](#maxlen)

Replace marker with value if string behind this one is less then poined in parameter.

```
use AndyDune\StringReplace\PowerReplace;

$string = 'Gogoriki go #one##two:masxlen(5):addcomma#';
$instance = new PowerReplace();

$instance->one = 'swim';
$instance->one = 'play';
$instance->replace($string);  // equals to 'Gogoriki go swim, play'

$instance->one = 'swim';
$instance->one = 'play games';
$instance->replace($string);  // equals to 'Gogoriki go swim'
```

### printf

[](#printf)

Print formatted string if it is not empty.

```
$string = 'I know words: #it:printf(«%s»):addcomma(1)##and_it:printf(«%s»):addcomma(1)# and #and_it_2:printf(«%s»):addcomma(1, 1)#';
$instance = new PowerReplace();
$instance->it = 'eat';
$instance->and_it_2 = 'sleep';
$instance->replace($string); // equals to  I know words: «eat» and «sleep»
```

### plural

[](#plural)

Pluralize the title for number.

```
$string = 'I see #count# #count:plural(man, men)#';
$instance = new PowerReplace();
$instance->count = 1;
$instance->replace($string); // I see 1 man
$instance->count = 21;
$instance->replace($string); // I see 21 men
```

### pluralrus

[](#pluralrus)

Russian pluralize the title for number.

```
$string = 'У меня есть #count# #count:pluralrus(яблоко, яблока, яблок)#';
$instance = new PowerReplace();

$instance->count = 1;
$instance->replace($string)); // У меня есть 1 яблоко

$instance->count = 21;
$instance->replace($string); // У меня есть 21 яблоко

$instance->count = 2;
$instance->replace($string); // У меня есть 2 яблока

$instance->count = 5;
$instance->replace($string); // У меня есть 5 яблок
```

### prefix

[](#prefix)

It shows given string as prefix only if value behind the key is not empty.

```
$string = 'Vegetables I have: #apple_count:prefix("apples "):addcomma(1)##orange_count:prefix("oranges "):addcomma(1)#';
$instance = new PowerReplace();
$instance->apple_count = 1;
$instance->replace($string); // Vegetables I have: apples 1
```

### postfix

[](#postfix)

It shows given string as postfix only if value behind the key is not empty.

```
$string = 'Params: #weight:prefix("weight: "):postfix(kg)##growth:prefix("growth: "):postfix(sm):addcomma#';
$instance = new PowerReplace();
$instance->weight = 80;
$instance->growth = 180;
$instance->replace($string); // Params: weight: 80kg, growth: 180sm
```

### showIfEqual

[](#showifequal)

It shows string given in second param if first param is equal to value behind the placeholder.

```
$string = 'Anton #weight:showIfEqual(80, "has normal weight")##weight:showIfEqual(180, "has obesity")#.';
$instance = new PowerReplace();
$instance->weight = 80;
$instance->replace($string); // Anton has normal weight.

$string = 'Anton #weight:showIfEqual(80, "has normal weight")##weight:showIfEqual(180, "has obesity")#.';
$instance = new PowerReplace();
$instance->weight = 180;
$instance->replace($string); // Anton has obesity.
```

### showIfOtherValueNotEmpty

[](#showifothervaluenotempty)

It shows string value behind the current placeholder if another is not empty.

```
$string = 'Variants #type[name]:showIfOtherNotEmpty(type[value])##type[value]:prefix(": ")#';
$instance = new PowerReplace();
$instance->setArray(['type'=> ['name' => 'color', 'value' => 'green']]);
$instance->replace($string); // Variants color: green
```

Custom Functions
----------------

[](#custom-functions)

You can add your own functions with replace rules. Markers and functions are not case sensitive.

```
$string = 'Where is #word:leftAndRight(_)#?';
// or the same
$string = 'Where is #WORD:LEFTANDRIGHT(_)#?';

$functionHolder = new FunctionsHolder();

// add custom function with name leftAndRight
$functionHolder->addFunction('leftAndRight', function ($string, $symbol = '') {
    return $symbol . $string . $symbol;
});
$instance = new PowerReplace($functionHolder);
$instance->word = 'center';
$instance->replace($string); // Where is _center_?

```

Application
-----------

[](#application)

- [HtmlTable](https://github.com/AndyDune/HtmlTable/blob/master/src/Builder.php)

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~6 days

Recently: every ~1 days

Total

17

Last Release

2835d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79da3b2173a2cefb36abc9b4707cf2c633df8f2c748633ccf64186f5c0e7be6c?d=identicon)[AndyDune](/maintainers/AndyDune)

---

Top Contributors

[![AndyDune](https://avatars.githubusercontent.com/u/3772910?v=4)](https://github.com/AndyDune "AndyDune (42 commits)")

---

Tags

phpstring-replacephparray

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[zakirullin/mess

Convenient array-related routine &amp; better type casting

21228.9k2](/packages/zakirullin-mess)

PHPackages © 2026

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