2

Let say I have the following string which I want to send as an email to a customer.

"Hello Mr/Mrs {{Name}}. You have subscribed for {{Service}} at {{Date}}."

And I have an array with the values that should be replaced

array(
    'Name' => $customerName, //or string
    'Service' => $serviceName, //or string
    'Date' => '2015-06-06'
);

I can find all strings between {{..}} with this:

preg_match_all('/\{{(.*?)\}}/',$a,$match);

where $match is an array with values, But I need to replace every match with a corresponding value from an array with values

Note that the array with values contains a lot more values and the number of items in it or the keys sequence is not relevant to number of matches in string.

Ivo
  • 353
  • 2
  • 13
  • As an alternative you can use an existing template engine which already does this. Or use a library that sends mail and accepts variables of the sort. – Alex Andrei Dec 01 '15 at 12:15

3 Answers3

3

You can use preg_replace_callback and pass the array with the help of use to the callback function:

$s = "Hello Mr/Mrs {{Name}}. You have subscribed for {{Service}} at {{Date}} {{I_DONT_KNOW_IT}}.";
$arr = array(
    'Name' => "customerName", //or string
    'Service' => "serviceName", //or string
    'Date' => '2015-06-06'
);
echo $res = preg_replace_callback('/{{(.*?)}}/', function($m) use ($arr) {
       return isset($arr[$m[1]]) ? $arr[$m[1]] : $m[0]; // If the key is uknown, just use the match value
    }, $s);
// => Hello Mr/Mrs customerName. You have subscribed for serviceName at 2015-06-06.

See IDEONE demo.

The $m[1] refers to what has been captured by the (.*?). I guess this pattern is sufficient for the current scenario (does not need unrolling as the strings it matches are relatively short).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Thank you. This is what i was looking for. – Ivo Dec 01 '15 at 12:17
  • I'd suggest `return isset($arr[$m[1]]) ? $arr[$m[1]] : '';` in case there are some tags not registered in the dictionary. – Amarnasan Dec 01 '15 at 12:21
  • @Amarnasan: Yes, a good suggestion. If the expected behavior is to just remove unknown tags, this will work. Ivo, please check if this is the expected behavior, and I will update the answer accordingly. – Wiktor Stribiżew Dec 01 '15 at 12:26
  • 1
    Yes I implemented that of course. Even I made it like this: return isset($arr[$m[1]]) ? $arr[$m[1]] : '{{'.$m[1].'}}'; So if there is no match it will return the {{key}} to be known that the given key is not proper – Ivo Dec 01 '15 at 12:27
  • I suggest just using `$m[0]` that stands for the whole matched text. In the example string, I added `{{I_DONT_KNOW_IT}}`, and it is output as is since there is no corresponding key in the dictionary. – Wiktor Stribiżew Dec 01 '15 at 12:30
1

You don't need to use a regex for that, you can do it with a simple replacement function if you change a little the array keys:

$corr = array(
    'Name' => $customerName, //or string
    'Service' => $serviceName, //or string
    'Date' => '2015-06-06'
);

$new_keys = array_map(function($i) { return '{{' . $i . '}}';}, array_keys($corr));
$trans = array_combine($new_keys, $corr);

$result = strtr($yourstring, $trans);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

Try

<?php

$str = "Hello Mr/Mrs {{Name}}. You have subscribed for {{Service}} at {{Date}}.";

$arr = array(
    'Name' => 'some Cust', //or string
    'Service' => 'Some Service', //or string
    'Date' => '2015-06-06'
);

$replaceKeys = array_map(
   function ($el) {
      return "{{{$el}}}";
   },
   array_keys($arr)
);

$str = str_replace($replaceKeys, array_values($arr), $str);

echo $str;
Ashish Choudhary
  • 2,004
  • 1
  • 18
  • 26