htmlspecialchars 删除格式
我在类中的另一个函数中调用 htmlspecialchars 函数,但是当我这样做时,即使显示数据,它也会删除在单行中显示数据的所有格式。
这是代码:
class Name {
. .. .
public function h($s)
{
echo htmlspecialchars($s, ENT_QUOTES);
}
public function formatQuotes($row)
{
return "<p id=\"ab_quotes\">" . this->h($row['cQuotes']) . "</p>"
. "<p id=\"ab_author\">" . this->h($row['vAuthor']) . "</p>";
}
}
如果我删除对 htmlspecialchars 函数的引用,它会按应有的方式显示数据。
更新:
这是我应用的CSS:
p#ab_quotes{
font-size: 22px;
word-wrap: break-word;
position: absolute;
top: 80px;
left: 5px;
padding: 8px 6px;
}
p#ab_author {
font-size: 15px;
position: absolute;
top: 200px;
right: 5px;
padding: 8px 6px;
text-transform: uppercase;
letter-spacing: 0.1em;
color: #EB3B55;
}
$row['cQuotes']和Author变量中没有html。这是在实现 htmlspecialchars 时删除的 css 格式。
我注意到的另一件事是,如果我删除 ENT_QUOTES
,它会起作用,但再次使用它,它会删除格式。为什么会这样呢?
i'm calling the htmlspecialchars function inside another function in a class, but when i do this, even though the data displays, it removes all formatting showing the data in a single line.
this is the code:
class Name {
. .. .
public function h($s)
{
echo htmlspecialchars($s, ENT_QUOTES);
}
public function formatQuotes($row)
{
return "<p id=\"ab_quotes\">" . this->h($row['cQuotes']) . "</p>"
. "<p id=\"ab_author\">" . this->h($row['vAuthor']) . "</p>";
}
}
if i remove the reference to htmlspecialchars function, it displays the data as it should.
UPDATE:
this is the css which i've applied:
p#ab_quotes{
font-size: 22px;
word-wrap: break-word;
position: absolute;
top: 80px;
left: 5px;
padding: 8px 6px;
}
p#ab_author {
font-size: 15px;
position: absolute;
top: 200px;
right: 5px;
padding: 8px 6px;
text-transform: uppercase;
letter-spacing: 0.1em;
color: #EB3B55;
}
there is no html within $row['cQuotes'] and Author variables. it is the css formatting which is removed when the htmlspecialchars is implemented.
another thing, that i noticed was that if i removed ENT_QUOTES
, it works, but again with it, it removes the formatting. why is this so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能是函数
h()
需要返回数据而不是回显它(基于您使用h()
结果的方式)The problem might be that the function
h()
needs to return the data rather than echo it (based on how you are using the result ofh()
)你说的格式化是什么意思?如果您指的是 HTML 代码,htmlspecialchars 会将这些对 HTML 至关重要的字符替换为其实体:
(取自此处:http://php.net/manual/en/function.htmlspecialchars .php)
当然,此时任何格式都不会生效。这就是 htmlspecialchars() 的意义所在。
What do you mean by formatting? If you mean HTML code, htmlspecialchars will replace these characters crucial to HTML with their entities:
(taken from here: http://php.net/manual/en/function.htmlspecialchars.php)
Of course none of the formatting will have effect then. That's the point of htmlspecialchars().