2

I have a simple PHP function that outputs HTML.

<?php
function get_header() {
?>
<div id="header">
  <div class="page-width">
  <!-- And a lot more HTML after this line. -->
<?php
}
?>

So, when I call get_header(), the function outputs the HTML.

What is the simplest option to tweak this function to return the HTML as a string? Do I need to create a wrapper around this function? In other words, I'd like to be able to do e.g. var html_string = get_header_wrapper(), where html_string contains all the HTML above.

One thing I could think of is to duplicate the function and make it return a string. However, that would be so inefficient because it introduces a lot of code duplicate.

<?php
function get_header_wrapper() {
  var ret = <<<EOD
  <div id="header">
    <div class="page-width">
    <!-- And a lot more HTML after this line. -->
  ...
  EOD;

  return ret;
}
?>
4
  • 2
    What's wrong with the second code block? (aside from the fact the EOD; should not be prepended with any whitespaces. Commented Jan 4, 2012 at 17:08
  • Why can't you modify the existing function? Commented Jan 4, 2012 at 17:08
  • @Truth: The second block of code will work just fine. However, I was looking for a solution where I don't have to duplicate that chunk of HTML code into a different function. Sorry, I wasn't clear that I have no privilege to modify the get_header() directly. Thanks. Commented Jan 4, 2012 at 17:15
  • Could be useful: what is output buffering? Commented Aug 4, 2012 at 9:24

3 Answers 3

8

You can make use of output bufferingDocs to get the output of that function:

ob_start();
get_header();
$html = ob_get_clean();

If you need that more than once, you can wrap it into a function of it's own:

/**
 * call a function and return it's output as string.
 * 
 * @param callback $function
 * @param array $arguments (optional)
 * @param var $return (optional) the return value of the callback
 * @return string function output
 */
function ob_get_call($function, array $arguments = array(), &$return = NULL)
{
    ob_start();
    $return = call_user_func_array($function, $arguments);
    $buffer = ob_get_clean();
    return $buffer;
}

Usage:

$html = ob_get_call('get_header');

As the answer is that popular today, here is another function to get the output of an include:

/**
 * include a file and return it's output as string.
 * 
 * @param string $file
 * @param array $variables (optional) keys as variable names and values as variable values
 * @param var $includeReturn (optional) the return value of the include
 * @return string function output
 */
function ob_get_include($file, array $variables = array(), &$includeReturn = NULL)
{
    $includeFilename = $file;
    unset($file);
    extract($variables);
    unset($variables);
    ob_start();
    $includeReturn = include($includeFilename);
    return ob_get_clean();
}

Usage:

include.php:

<div class="greeting">
    Hello <em><?php echo htmlspecialchars($name); ?></em>!
</div>

Using:

$variables = array(
    'name' => 'Marianne',
);
$html = ob_get_include('include.php', $vars);

Related:

Sign up to request clarification or add additional context in comments.

1 Comment

Also +1 for the very detailed help. Thank you!
1

Use output buffering:

<?php
function get_header() {
  ob_start();
  <div id="header">
    <div class="page-width">
    <!-- And a lot more HTML after this line. -->
  ...
  $content = ob_get_contents();
  ob_end_clean();
  return $content;
}
?>

You can even do some processing on the string before returning it. OB rocks!

1 Comment

Using output buffering in this way is completely overkill. Plus you're modifying the original function.
0

The best action to get the content of a html-content function is the following and then it should be at your hand:

function html_content(){
ob_start();?>
    <div class="some-div" id='the_id'>html text goes here
    </div>
<?php 
return ob_get_clean();
}

In this method, you can easily copy paste the needed html tags and content to your codes and easily use them.

I've used ob_start(); before the start of html tags and at the end I used return ob_get_clean();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.