fork download
  1. <?php
  2.  
  3. $str = 'This thing should output _$this_key$_ and also _$this_one$_ so that I can match it with an array!';
  4.  
  5. $replacements = [
  6. 'this_key' => 'love',
  7. 'this_one' => 'more love'
  8. ];
  9.  
  10. $replaced = preg_replace_callback('/_\$([^$]+)\$_/', function($matches) use ($replacements) {
  11. return $replacements[$matches[1]];
  12. }, $str);
  13.  
  14. print $replaced;
Success #stdin #stdout 0.01s 24508KB
stdin
Standard input is empty
stdout
This thing should output love and also more love so that I can match it with an array!