Вы можете использовать выходную буферизацию .
// start output buffering, nothing will be printed out to the screen from now.
ob_start();
// this will not print to the screen as usual because of ob_start().
include('my/template/file.php');
// ob_get_contents() will return everything that should have been sent to the screen since ob_start was called.
$template_contents = ob_get_contents();
$final_contents = doMyStringReplaceFunction($template_contents, $arrayOfSubstitutions);
Вы также можете сделать это сразу, но с помощью обратного вызова on_start ();
ob_start('myCallbackFunction');
include('my/template/file.php');
// this will pass the contents of the include to myCallbackFunction() then echo to the screen.
ob_end_flush();