How to search and insert files through conditionally {{}}?

Hello!
Please make sure all files have {{}} and if there is such a syntax, to insert in this place the text like this: {{blog/flowers.txt}}?
Ie if the file is {{}}, it'll substitute it in the text from the specified file.
March 23rd 20 at 19:04
3 answers
March 23rd 20 at 19:06
Solution
The regular season {{(.*?)}} looking for these your parenthesis (example)
and returns what is between them.

Having found them, you need to try to get data from a file (file_get_conents)

And paste it instead of all this construction with brackets (str_replace)
thank you! - annalise_Spinka58 commented on March 23rd 20 at 19:09
March 23rd 20 at 19:08
Solution
$str = 'Original text and {{}}.';
$dir = 'blog\flowers.txt';
$str = preg_replace_callback(
'~{{\K(?=}})~',
function()use($dir){
 return file_get_contents($dir);
},
$str
);

echo $str;

But such substitutions are more appropriate tools:
$str = 'Original text and {{}}.';
$dir = 'blog\flowers.txt';
$str = str_replace('{{}}', '{{'. file_get_contents($dir) . '}}', $str);

echo $str;
absolutely amazing! - annalise_Spinka58 commented on March 23rd 20 at 19:11
March 23rd 20 at 19:10
Solution
file_get_contents - readable contents of the file into a string
preg_replace_callback - model portions of the row

Find more questions by tags PHP