1

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; ?> 
7
  • 5
    Where is the code that renders the variable? Sounds like it's being HTML encoded. Commented Oct 11, 2012 at 12:32
  • 1
    How do you output the variable? Commented Oct 11, 2012 at 12:32
  • 1
    The problem is in how you output the variable, and you haven't show us that code. Commented Oct 11, 2012 at 12:32
  • 1
    @Zathrus, I think when MJQ says "doesn't execute the html" he means that the HTML is not being treated as HTML by the browser, but instead is being displayed verbatim by the browser. I agree with other comments, that how the variable is rendered / output into the HTML is probably the issue Commented Oct 11, 2012 at 12:37
  • 2
    @MJQ: Please edit the question to show that code. Not everyone reads all the comments, especially once they don't all show by default. Commented Oct 11, 2012 at 12:56

3 Answers 3

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

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

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

Comments

1

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(); ?>

Comments

1

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;
    ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.