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.

5
  • Use heredoc, like this: pastebin.com/jsjWuyuL Commented Aug 19, 2013 at 8:35
  • @DipeshParmar I am not talking about converting < or > to &lt; or &gt; I want to return the html response as string. Commented Aug 19, 2013 at 8: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. Commented Aug 19, 2013 at 8:37
  • 1
    use a mvc framework so you don't have to write html and php mixed too much Commented Aug 19, 2013 at 8:37
  • EDIT: looks like I can comment after all ;) I can't comment yet, so I'll post as an answer. stackoverflow.com/questions/8751182/include-php-file-as-string Commented Aug 19, 2013 at 8:42

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@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.
0

Use output buffer:

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

  ...

  return ob_get_clean();
}

Changed from ob_get_flush() to ob_get_clean();

6 Comments

This does not make sense, because he is not using echo, but initializes a string variable and returns it.
@Julian You need to read the question, not just skip it. This is a valid method that will do what the OP asked.
This answer makes perfect sense. However, ob_get_clean may be a better choice.
@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.
I tested your solution it is giving output two times
|

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.