0

Is there anyway to convert an html response to string in php. let me expalin through an example.

<?php
function printTitle($title="Welcome"){
?>
<div class='mainTitle'>
<div class='titleLogo'>

</div>
<div class='titleString'>
<?php echo $title; ?>
</div>
</div>

<?php
}
?>

Here calling this function anywhere will output html showing title. Now what I need is to convert this response into string value so that it can be passed as json response like:

$response=array("title"=>printTitle(),"sidebar"=>getSideBar());

echo json_encode($response);

I want to do it like this so that I can fetch title and sidebar via ajax.

One way to do it is like:

<?php
    function printTitle($title="Welcome"){

    $ret="<div class='mainTitle'>
    <div class='titleLogo'>

    </div>
    <div class='titleString'>
    ". $title ."
    </div>
    </div>";

    return $ret;
    }
    ?>

but this makes html really a mess.

Manish
  • 1,946
  • 2
  • 24
  • 36
  • Use [heredoc](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc), like this: http://pastebin.com/jsjWuyuL – Passerby Aug 19 '13 at 08:35
  • @DipeshParmar I am not talking about converting < or > to < or > I want to return the html response as string. – Manish Aug 19 '13 at 08:36
  • What do you get in the console when you use `console.log` in your JavaScript? You should, first of all, remove the line breaks. PHP won't parse this. – Julian F. Weinert Aug 19 '13 at 08:37
  • 1
    use a mvc framework so you don't have to write html and php mixed too much – DevZer0 Aug 19 '13 at 08:37
  • EDIT: looks like I can comment after all ;) I can't comment yet, so I'll post as an answer. http://stackoverflow.com/questions/8751182/include-php-file-as-string – Dexa Aug 19 '13 at 08:42

2 Answers2

2

You can use a HEREDOC:

function printTitle($title="Welcome"){
    return <<<HTML
<div class='mainTitle'>
    <div class='titleLogo'>
    </div>
    <div class='titleString'>
    $title
    </div>
</div>
HTML;
}
Benubird
  • 18,551
  • 27
  • 90
  • 141
  • @manish that is because you are outputting it to the browser directly. Use the view source option in your browser to see what it actually outputs, or use htmlentites() to view it in the browser. – Benubird Aug 19 '13 at 12:02
0

Use output buffer:

function printTitle($title="Welcome"){
  ob_start();

  ...

  return ob_get_clean();
}

Changed from ob_get_flush() to ob_get_clean();

Nils
  • 436
  • 2
  • 5
  • This does not make sense, because he is not using `echo`, but initializes a string variable and returns it. – Julian F. Weinert Aug 19 '13 at 08:36
  • @Julian You need to read the question, not just skip it. This is a valid method that will do what the OP asked. – Benubird Aug 19 '13 at 08:38
  • This answer makes perfect sense. However, [ob_get_clean](http://www.php.net/manual/en/function.ob-get-clean.php) may be a better choice. – webbiedave Aug 19 '13 at 08:44
  • @user2689155 I may be asking too much but can you please give an example, I tried what you told but not getting how to use it. – Manish Aug 19 '13 at 09:01
  • I tested your solution it is giving output two times – Manish Aug 19 '13 at 09:20
  • @manish it is outputting twice, because ob_get_flush flushes (outputs) the buffer as well as returning it. You want ob_get_clean, like webbiedave said. – Benubird Aug 19 '13 at 11:27