如何阻止 html TEXTAREA 解码 html 实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8519070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop an html TEXTAREA from decoding html entities
提问by Rami Dabain
I have a strange problem:
我有一个奇怪的问题:
In the database, I have a literal ampersand lt semicolon:
在数据库中,我有一个文字和号 lt 分号:
<div
whenever its printed into a html textarea tag, the source code of the page shows the >
as >
.
每当它打印到 html textarea 标签中时,页面的源代码都会显示>
为>
.
How do I stop this decoding?
如何停止此解码?
采纳答案by Chris Hubbard
In PHP, this can be done using htmlentities(). Example below.
在 PHP 中,这可以使用 htmlentities() 来完成。下面举例。
<?php
$content = "This string contains the TM symbol: ™";
print "<textarea>". htmlentities($content) ."</textarea>";
?>
Without htmlentities(), the textarea would interpret and display the TM symbol (™) instead of "™".
如果没有 htmlentities(),textarea 将解释并显示 TM 符号 (™) 而不是“™”。
回答by Quentin
You can't stop entities being decoded in a textarea[1] since the content of a textarea is not(unlike a script or style element) intrinsic CDATA, even though error recovery may sometimesgive the impression that it is.
您无法停止在 textarea[1] 中解码实体,因为 textarea 的内容不是(与脚本或样式元素不同)固有的 CDATA,即使错误恢复有时可能给人的印象是它。
The definition of the textarea element is:
textarea 元素的定义是:
<!ELEMENT TEXTAREA - - (#PCDATA) -- multi-line text field -->
i.e. it contains PCDATA which is described as:
即它包含被描述为的PCDATA :
Document text (indicated by the SGML construct "#PCDATA"). Text may contain character references. Recall that these begin with
&
and end with a semicolon (e.g.,Hergé's adventures of Tintin
contains the character entity reference for the e acutecharacter).
文档文本(由 SGML 构造“#PCDATA”指示)。文本可能包含字符引用。回想一下,这些
&
以分号开头和结尾(例如,Hergé's adventures of Tintin
包含e 尖字符的字符实体引用)。
This means that when you type (the invalid HTML of) "start of tag" (<
) the browser corrects it to "less than sign" (<
) but when you type "start of entity" (&
), which isallowed, no error correction takes place.
这意味着当您输入(无效的 HTML)“标签开始”(<
)时,浏览器将其更正为“小于号”(<
),但当您输入“实体开始”(&
)时,这是允许的,没有错误更正发生。
You need to write what you mean. If you want to include some HTML as data then you must convert any character with special meaning to its respective character reference.
你需要写下你的意思。如果您想包含一些 HTML 作为数据,那么您必须将任何具有特殊含义的字符转换为其各自的字符引用。
If the data is:
如果数据是:
<div
Then the HTML must be:
那么 HTML 必须是:
<textarea>&lt;div</textarea>
You can use the standard functions for converting this (e.g. PHP's htmlspecialchars
or Perl's HTML::Entities module).
您可以使用标准函数来转换它(例如 PHPhtmlspecialchars
或 Perl 的HTML::Entities 模块)。
NB 1: If you were using XHTML[2] (and really using it, it doesn't count if you serve it as text/html) then you could use an explicit CDATA block:
NB 1:如果您使用的是 XHTML[2] (并且真的使用它,如果您将其作为 text/html 提供,则不算数),那么您可以使用显式 CDATA 块:
<textarea><![CDATA[<div]]></textarea>
NB 2: Or if browsers implemented HTML 4 correctly
NB 2:或者浏览器是否正确实现了 HTML 4
Ok , but the question is . why it decodes them anyway ? assuming i've added & , save the textarea , ti will be saved < , but displayed as < , saving it again will convert it back to < (but it will remain < in the database) , saving again will save it a < in the database , why the textarea decodes it ?
好的,但问题是。为什么它解码它们呢?假设我添加了 & ,保存 textarea , ti 将被保存 < ,但显示为 < ,再次保存会将其转换回 <(但它将保留在数据库中),再次保存会将其保存为 < 在数据库中,为什么 textarea 解码它?
- The serversends (to the browser) data encoded as HTML.
- The browsersends (to the server) data encoded as application/x-www-form-urlencoded(or multipart/form-data).
- 所述服务器发送(到浏览器)编码为数据HTML。
- 所述浏览器发送(到服务器)编码为数据应用程序/ x-WWW窗体-urlencoded(或多部分/格式的数据)。
Since the browser is not sending the data as HTML, the characters are not represented as HTML entities.
由于浏览器不是以 HTML 形式发送数据,因此字符不表示为 HTML 实体。
If you take the data received from the client and then put it into an HTML document, then you must encode it as HTML first.
如果你把从客户端接收到的数据放入一个 HTML 文档中,那么你必须先将它编码为 HTML。
回答by Victor
You can serve your DB-content from a separate page and then place it in the textarea using a Javascript (jQuery) Ajax-call:
您可以从单独的页面提供您的数据库内容,然后使用 Javascript (jQuery) Ajax 调用将其放置在 textarea 中:
request = $.ajax
({
type: "GET",
url: "url-with-the-troubled-content.php",
success: function(data)
{
document.getElementById('id-of-text-area').value = data;
}
});
Explained at
解释于
http://www.endtask.net/how-to-prevent-a-textarea-element-from-decoding-html-entities/
http://www.endtask.net/how-to-prevent-a-textarea-element-from-decoding-html-entities/
回答by Lukas Eder
You have to be sure that this is rendered to the browser:
您必须确保将其呈现给浏览器:
<textarea name="somename">&lt;div</textarea>
Essentially, this means that the &
in <
has to be html encoded to &
. How to do it will depend on the technologies you're using.
本质上,这意味着&
in<
必须是 html 编码为&
. 如何做到这一点取决于您使用的技术。
UPDATE: Think about it like this. If you want to display<div>
inside a textarea, you'll have to encode <>
because otherwise, <div>
would be a normal HTML element to the browser:
更新:这样想。如果要在 textarea 内显示<div>
,则必须进行编码<>
,否则<div>
将是浏览器的普通 HTML 元素:
<textarea name="somename"><div></textarea>
Having said this, if you want to display<div>
inside a textarea, you'll have to encode &
again, because the browser decodes HTML entities when rendering HTML. It has nothing to do with your database.
话虽如此,如果你想在 textarea 内显示<div>
,你必须&
再次编码,因为浏览器在呈现 HTML 时会解码 HTML 实体。它与您的数据库无关。
回答by Andreas Jansson
I had the same problem and I just made two replacements on the text to show from the database before letting it into the text area:
我遇到了同样的问题,我只是在文本上进行了两次替换以从数据库中显示出来,然后再将其放入文本区域:
myString = Replace(myString, "&", "&")
myString = Replace(myString, "<", "<")
Replace n:o 1 to trick the textarea to show the codes. replace n:o 2: Without this replacement you can not show the word "" inside the textarea (it would end the textarea tag).
替换 n:o 1 以欺骗 textarea 以显示代码。替换 n:o 2:没有这个替换,你不能在 textarea 中显示单词 ""(它会结束 textarea 标签)。
(Asp / vbscript code above, translate to a replace method of your language choice)
(上面的Asp / vbscript代码,翻译成你选择的语言的替换方法)
回答by Lewis Richard Phillip Cowles
I found an alternative solution for reading and working with in-browser, simply read the element's text() using jQuery, it returns the characters as display characters and allows me to write from a textarea to a div's innerHTML using the property via html()...
我找到了一种在浏览器中阅读和使用的替代解决方案,只需使用 jQuery 读取元素的 text(),它将字符作为显示字符返回,并允许我使用属性通过 html() 从 textarea 写入 div 的 innerHTML ...