I would make a simple template class to parse it,
class SimpleTemplate
{
protected $src;
protected $vars;
public function __construct($src)
{
$this->src = $src;
}
public function assign($key, $value)
{
$this->vars[$key] = $value;
}
public function out()
{
return str_replace(array_keys($this->vars),$this->vars,$this->src);
}
}
$T = new SimpleTemplate("We have received ##AMOUNT## ##CURRENCY## for your OrderID n. ##ORDER_ID##");
$T->assign('##AMOUNT##', 'foo');
$T->assign('##CURRENCY##', 'bar');
$T->assign('##ORDER_ID##', 'hello');
echo $T->out();
Outputs:
"We have received foo bar for your OrderID n. hello"
You can see it here live:
And you can always expand on it and add features, like removing the ## from the assignment, and removing any un-used tags. etc...
Basically it's just an array management tool for your variables. Because we are building an array like this:
[
'##AMOUNT##' => 'foo',
'##CURRENCY##' => 'bar',
'##ORDER_ID##' => 'hello'
];
And then using str_replace we use the keys as the search, the value as the replacement value.
If you want to remove the ## from the tag assignment just change this:
public function assign($key, $value)
{
$this->vars['##'.$key.'##'] = $value;
}
And then call like this:
$T->assign('AMOUNT', 'foo');
The real benefit here, is having a class to implement your logic in. It keeps it clean and re-usable.
UPDATE
As @LawrenceCherone mentioned in the comments, you can get a cleaner interface by using a couple magic methods.
class SimpleTemplate
{
protected $src;
protected $vars;
public function __construct($src)
{
$this->src = $src;
}
public function __set($key, $value)
{
$this->vars['##'.strtoupper($key).'##'] = $value;
}
public function __toString()
{
return str_replace(array_keys($this->vars),$this->vars,$this->src);
}
}
$T = new SimpleTemplate("We have received ##AMOUNT## ##CURRENCY## for your OrderID n. ##ORDER_ID##");
$T->Amount = 'foo'; //any casing works because of the transforms, in __set
$T->CURRENCY = 'bar';
$T->order_id = 'hello';
echo $T; //using __toString() lets us just output the class as our result string.
Specifically __set and __toString. For this one I did a few transforms on the key so as to make it a bit less error prone, and a bit less ugly. For example the use of Mixed Casing for the dynamic Properties. By using strtoupper they can be input in any case, and removing the # prevents having to wrap them like this $T->{'##AMOUNT##'} = 'foo';
The last thing I will add, is if you can have "tags" that are not assigned but want them removed you can use this regx to remove them.
public function __toString()
{
$str = str_replace(array_keys($this->vars),$this->vars,$this->src);
return preg_replace(['/##\w+##/', '/\s{2,}/'], ['', ' '], $str); //remove any left over ##{word}## tags, and compress any run-on spaces.
}
You can see an example of this here
The second one in the array /\s{2,}/, just takes run on spaces and compresses them to a single space, so if you have like 4 spaces it becomes a single space. It's not well documented but preg_replace can take an array just like str_replace does.
This is why I suggest building up a class, it gives you a nice place to implement all this "stuff" if you want, and not have to keep re-writing it all the time.