PHP code example of andydune / string-replace

1. Go to this page and download the library: Download andydune/string-replace library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andydune / string-replace example snippets


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' 


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' 


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

use AndyDune\StringReplace\PowerReplace;

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

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

$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'


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'

$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»

$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

$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 яблок

$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

$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


$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.

$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


$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_?


php composer.phar update