Last active
December 14, 2015 03:19
-
-
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 )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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