- preg_replace_callback: documentation ( source)
<?php
$body = "This is a body of {{awesome}} text {{blabla}} from a book.";
//$count = 0;
$terms[] = '1';
$terms[] = '2';
echo preg_replace_callback('/\{{2}(.*?)\}{2}/',function($match) use (&$count,$terms) {
$return = !empty($terms[$count]) ? $terms[$count] : 'Default value for unknown term';
// $count++;
echo "#$count#";
return $return;
}, $body, -1, $count);