user1559230
user1559230

Reputation: 2811

How to save a html with php function and html tags into a php variable as string?

<div class="readstar">
    <div class="stars">
    <?php if(is_active_sidebar('callout-box1')): ?>
    <?php $rating = get_average_rating($post->ID);echo num_to_stars($rating, 1);  ?>
    </div>
    <a class="readmore" href="<?php the_permalink();?> " >
    <img  src="<?php bloginfo('template_url');?>/images/readmore_2.png" alt="read more" />
    </a>
    <?php endif; ?> 
</div>

I have a html posted below. I want to save it into a php variable as string with html tags and php functions inside it. I have tried this

$str='<div class="readstar">
<div class="stars">
TEST TEST
</div>';

It only creates a string TEST TEST. But I need the whole html with tags. Please help me.

Upvotes: 1

Views: 1344

Answers (2)

Michal
Michal

Reputation: 33

The code that you provided is writing the entire string to $str. When echoing it you should use htmlspecialchars()

Upvotes: 3

Marc B
Marc B

Reputation: 360692

You want to execute that php code and save the generated html? Simplest method is to use output buffering:

ob_start();
... your html+php code here ...
$html = ob_get_clean();

As well, note that if you're viewing this output in a browser, the browser is going to RENDER the html tags. Try 'view source' instead, to get the raw page contents, including the tags.

Upvotes: 0

Related Questions