htmlspecialchars 在数据内容中不起作用
htmlspecialchars does not work inside data-content
我正在使用 bootstrap select 来显示类别,并且我正在使用 htmlspecialchars 清理这些类别,但由于某些原因,当它在数据内容属性中时它不会没用,因为我有一个名为的类别,浏览器将其解释为真正的 html 标记而不是字符串。我曾尝试使用 htmlentitites 但它也不起作用...我也考虑过使用 urlencode 但是因为我使用的是 bootstrap select 我不知道在哪里解密它...
这是我的代码:
"<option value='$row[id]' data-content='<span class=\"glyphicon glyphicon-stop\"
style=\"color:$row[cor]\"></span> <span style=\"text-align:left\">
".htmlspecialchars("$row[nome]",ENT_QUOTES)."
</span>'>".htmlspecialchars("$row[nome]",ENT_QUOTES)."</option>";
As you can see all categories are showing (they have wierd names for testing purposes) but the last one that has the name of </a>
is not showing up and has you can see in the source the </a>
is being interpred as html and not as a string as the other categorie names
谢谢!
A <
被解释为 HTML 标记的开始。
<
是 HTML 编码的实体,将被解释为字符“<”。
如果将类似于 HTML 的内容放入属性中,则需要对其进行 HTML 编码以避免破坏 HTML 属性。例如,属性用引号括起来,如果你想在属性中使用引号作为值,则需要 "
:
attribute=""value""
bootstrap-select 使用 data-content
属性的值作为 HTML.
所以 data-content="<span>"
最终会成为 HTML 元素 <span>
.
如果您希望它以 文本“”结束,您需要提供呈现文本“”的 HTML,即 <span>
。由于您将其放入 HTML 属性中,因此您需要 HTML 将 that 编码为:
data-content="&lt;span&gt;"
☝️ 这是 <span>
的正确 HTML 编码值(因为它在属性中),当解释为 HTML 时(如 bootstrap-select 会做) 呈现为文本“”。
所以:
$attrTemplate = '<span class="glyphicon glyphicon-stop" style="color:%s"></span>
<span style="text-align:left">%s</span>';
$attr = sprintf($attrTemplate, htmlspecialchars($row['cor']), htmlspecialchars($row['nome']));
$optionTemplate = '<option value="%s" data-content="%s">%s</option>';
printf($optionTemplate, htmlspecialchars($row['id']), htmlspecialchars($attr), htmlspecialchars($row['nome']));
如您所见,进入 HTML 的所有内容都必须按原样保留 HTML 编码(此处使用 htmlspecialchars
)。
另见 The Great Escapism (Or: What You Need To Know To Work With Text Within Text)。
我正在使用 bootstrap select 来显示类别,并且我正在使用 htmlspecialchars 清理这些类别,但由于某些原因,当它在数据内容属性中时它不会没用,因为我有一个名为的类别,浏览器将其解释为真正的 html 标记而不是字符串。我曾尝试使用 htmlentitites 但它也不起作用...我也考虑过使用 urlencode 但是因为我使用的是 bootstrap select 我不知道在哪里解密它...
这是我的代码:
"<option value='$row[id]' data-content='<span class=\"glyphicon glyphicon-stop\"
style=\"color:$row[cor]\"></span> <span style=\"text-align:left\">
".htmlspecialchars("$row[nome]",ENT_QUOTES)."
</span>'>".htmlspecialchars("$row[nome]",ENT_QUOTES)."</option>";
As you can see all categories are showing (they have wierd names for testing purposes) but the last one that has the name of </a>
is not showing up and has you can see in the source the </a>
is being interpred as html and not as a string as the other categorie names
谢谢!
A <
被解释为 HTML 标记的开始。
<
是 HTML 编码的实体,将被解释为字符“<”。
如果将类似于 HTML 的内容放入属性中,则需要对其进行 HTML 编码以避免破坏 HTML 属性。例如,属性用引号括起来,如果你想在属性中使用引号作为值,则需要 "
:
attribute=""value""
bootstrap-select 使用 data-content
属性的值作为 HTML.
所以 data-content="<span>"
最终会成为 HTML 元素 <span>
.
如果您希望它以 文本“”结束,您需要提供呈现文本“”的 HTML,即 <span>
。由于您将其放入 HTML 属性中,因此您需要 HTML 将 that 编码为:
data-content="&lt;span&gt;"
☝️ 这是 <span>
的正确 HTML 编码值(因为它在属性中),当解释为 HTML 时(如 bootstrap-select 会做) 呈现为文本“”。
所以:
$attrTemplate = '<span class="glyphicon glyphicon-stop" style="color:%s"></span>
<span style="text-align:left">%s</span>';
$attr = sprintf($attrTemplate, htmlspecialchars($row['cor']), htmlspecialchars($row['nome']));
$optionTemplate = '<option value="%s" data-content="%s">%s</option>';
printf($optionTemplate, htmlspecialchars($row['id']), htmlspecialchars($attr), htmlspecialchars($row['nome']));
如您所见,进入 HTML 的所有内容都必须按原样保留 HTML 编码(此处使用 htmlspecialchars
)。
另见 The Great Escapism (Or: What You Need To Know To Work With Text Within Text)。