PHP code example of visual-craft / string-interpolator

1. Go to this page and download the library: Download visual-craft/string-interpolator 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/ */

    

visual-craft / string-interpolator example snippets


use VisualCraft\StringInterpolator\StringInterpolator;

$interpolator = new StringInterpolator();

$result = $interpolator->interpolate('Demonstration $var1 $var2.', [
    'var1' => 'of',
    'var2' => 'interpolation',
]);
// $result === "Demonstration of interpolation."

$result = $interpolator->interpolate('test${var1}test', [
    'var1' => '123',
]);
// $result === "test123test"

$result = $interpolator->interpolate('$var1 \$var2', [
    'var1' => '123',
]);
// $result === "123 $var2"

$result = $interpolator->interpolate('$var1 $var2', [
    'var1' => '123',
]);
// PHP Fatal error:  Uncaught VisualCraft\StringInterpolator\MissingVariableException: Missing variable 'var2'

$result = $interpolator->getVariablesNames('Demonstration $var1 $var2.');
// $result === ['var1', 'var2']

$result = $interpolator->getVariablesCounts('Demonstration $var1 $var2. $var1');
// $result === ['var1' => 2, 'var2' => 1]