Skip to content

Instantly share code, notes, and snippets.

@kzar
Last active December 14, 2015 03:19
Show Gist options
  • Select an option

  • Save kzar/5020154 to your computer and use it in GitHub Desktop.

Select an option

Save kzar/5020154 to your computer and use it in GitHub Desktop.
PHP return_printed function that takes a closure and returns as a string anything that was printed by it. (Thanks to Viper-7 in ##php, see his version that supports arguments http://codepad.viper-7.com/7ZSncY )
function return_printed() {
$args = func_get_args();
$f = array_shift($args);
if ($f) {
ob_start();
call_user_func_array($f, $args);
return ob_get_clean();
}
}
// return_printed(function($s) { print "hello world" . $s; }, " - argument");
// - "hello world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment