1
<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.

2 Answers 2

3

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

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

1 Comment

@user1559230 you are already saving the html correctly. But I assume because you are viewing it on a browser, the browser interprets the html so you only see the text in the div. if you want to see the html code as well use htmlspecialchars() as suggested by Michal
0

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.

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.