htmlspecialchars 删除格式

发布于 2024-09-11 10:41:47 字数 1085 浏览 2 评论 0原文

我在类中的另一个函数中调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-09-18 10:41:47

问题可能是函数 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 of h())

随遇而安 2024-09-18 10:41:47

你说的格式化是什么意思?如果您指的是 HTML 代码,htmlspecialchars 会将这些对 HTML 至关重要的字符替换为其实体:

  • “&” (& 符号)变为“&”
  • 当未设置 ENT_NOQUOTES 时,'"'(双引号)将变为 '"'。
  • 仅当设置了 ENT_QUOTES 时,'''(单引号)才变为 '''。
  • '<' (小于)变为“<”
  • '>' (大于)变为“>”

(取自此处: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:

  • '&' (ampersand) becomes '&'
  • '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
  • ''' (single quote) becomes ''' only when ENT_QUOTES is set.
  • '<' (less than) becomes '<'
  • '>' (greater than) becomes '>'

(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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文