将php变量设置为html

时间:2022-08-26 22:38:08

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

我将php变量的值设置为某些html。即

$_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?

然后在br标签后面的html中显示变量。但它不会在其中执行html。而是显示它像 hehehehehe 。那么,我的代码有什么问题!我该怎么办?

Here is the code that displays that IN HTML,

这是显示IN HTML的代码,

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

3 个解决方案

#1


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

这是显示 getComment())的代码:?>
escapeHtml($ _ item-> getComment(),array('b' ,'br','强','我','你'))?>

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.

我不知道$ this-> escapeHtml函数究竟做了什么,但它似乎是对字符串进行HTML编码。

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.

结果是任何标记,例如将作为< a>发送到浏览器。浏览器将显示为。浏览器不会将其视为标记,因此不会将其视为标记。

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

所以简单的答案是:不要编码HTML ...

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

#2


3  

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

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

这很好用! html被“执行”并显示链接。

#3


1  

I suspect you are just echoing the variable.

我怀疑你只是回应变量。

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

您需要使用如下所示的'htmlspecialchars'方法。

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

#1


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

这是显示 getComment())的代码:?>
escapeHtml($ _ item-> getComment(),array('b' ,'br','强','我','你'))?>

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.

我不知道$ this-> escapeHtml函数究竟做了什么,但它似乎是对字符串进行HTML编码。

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.

结果是任何标记,例如将作为< a>发送到浏览器。浏览器将显示为。浏览器不会将其视为标记,因此不会将其视为标记。

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

所以简单的答案是:不要编码HTML ...

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

#2


3  

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

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

这很好用! html被“执行”并显示链接。

#3


1  

I suspect you are just echoing the variable.

我怀疑你只是回应变量。

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

您需要使用如下所示的'htmlspecialchars'方法。

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