MJQ
MJQ

Reputation: 1786

Setting a php variable to html

I am setting the value of a php variable to some html. i.e.

$_img = '<a href="some url">hehehehehe</a>';

The variable is then shown in html after a br tag. But it doesn't execute the html in it. Rather it displays it like <a href="some url">hehehehehe</a>. So, is there any problem in my code! How can i do this thing?

Here is the code that displays that IN HTML,

 <?php if ($_item->getComment()): ?> <br/><?php echo $this->escapeHtml($_item->getComment(), array('b','br','strong','i','u')) ?> <?php endif; ?> 

Upvotes: 1

Views: 155

Answers (3)

freefaller
freefaller

Reputation: 19963

From your comment....

Here is the code that displays that <?php if ($_item->getComment()): ?> <br/><?php echo $this->escapeHtml($_item->getComment(), array('b','br','strong','i','u')) ?> <?php endif; ?>

As predicted by many people, it looks like you are encoding the value when you display it.

I don't know what the $this->escapeHtml function is doing exactly, but it would appear to be doing an HTML Encoding on the string.

The result being that any tag, for example <a> will be sent to the browser as &lt;a&gt; which the browser will display as <a>. The browser will not see it as a tag, and will therefore not treat it as one.

So the simple answer is: don't encode the HTML...

<?php echo $_item->getComment(); ?>

Upvotes: 1

Kal
Kal

Reputation: 2309

I suspect you are just echoing the variable.

You need use the 'htmlspecialchars' method such as below.

    <?php
    $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
    echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
    ?>

Upvotes: 1

kapoko
kapoko

Reputation: 988

<?php 
    $string = '<a href="http://stackoverflow.com">Hehehe</a>';
    echo $string;
?>

This works fine! The html is 'executed' and the link is displayed.

Upvotes: 3

Related Questions