<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nate&#039;s blog &#187; capture</title>
	<atom:link href="http://www.natenewz.com/tag/capture/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.natenewz.com</link>
	<description>My various projects</description>
	<lastBuildDate>Wed, 27 Oct 2010 00:07:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Force PHP function to return instead of echo</title>
		<link>http://www.natenewz.com/2010/06/09/force-php-function-to-return-instead-of-echo/</link>
		<comments>http://www.natenewz.com/2010/06/09/force-php-function-to-return-instead-of-echo/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 00:15:06 +0000</pubDate>
		<dc:creator>nate</dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[capture]]></category>
		<category><![CDATA[draw]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[return]]></category>

		<guid isPermaLink="false">http://www.natenewz.com/?p=347</guid>
		<description><![CDATA[Have you ever working on some PHP software that had functions that echo&#8217;ed and you want them to return so you can echo later or pass the data to something else? I found a nice way of presenting the solution. All of the php functions that start with ob_ such as ob_start() allow you to [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever working on some PHP software that had functions that echo&#8217;ed and you want them to return so you can echo later or pass the data to something else? I found a nice way of presenting the solution. All of the php functions that start with ob_ such as ob_start() allow you to do this, but it takes several lines of code, and doesn&#8217;t seem very clean. I&#8217;ve put these into a a function that can be used as follows:</p>
<pre id='PHP' class="prettyprint" >
/**
 * Captures output from a function
 *
 * @param $callback is the name of the function
 * @return the output of the function
 */
function capture($callback)
{
    $args = func_get_args();
    array_shift($args);
    ob_start();
    if (count($args))
        call_user_func_array($callback, $args);
    else
        call_user_func($callback);
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}
$sidebar = capture('get_sidebar');
// or if it was a function you needed to pass arguments into
$title = 'Test';
$author = 'Nate Nuzum';
$header = capture('get_header', $title, $author);

// works in the newer version of php 5
$sidebar = capture(function()
{
    get_sidebar();
});
function get_sidebar()
{
    echo 'hello';
}
function get_header($title, $author)
{
    echo "{$title} {$author}";
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.natenewz.com/2010/06/09/force-php-function-to-return-instead-of-echo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

