为什么CRLF在使用php分配给<input />时转换为LF

时间:2015-04-05 15:12:23

标签: javascript php html dom

(1)一个表单包括textarea,带有换行符的类型文本。喜欢:

一个
B'/ P>

然后提交表单并将文本存储到数据库。内容的长度为4.我可以使用ord()函数输出他们的ascii代码。他们是 97(a) 13(cr) 10(lf) 98(b)

(2)获取内容并使用smarty.Like:

将它们分配给模板
//$string is get from database.
$smarty->assign('str', $string);

使用input元素将内容存储在html模板中。

<input id="test" type="hidden" value="{$str}">

(3)获取输入值的长度

document.getElementById("test").value.length;    //the result is 3

使用

{$str|@strlen}    //the result is 4

如果我提交表格并获得cotents的ascii代码,则 97(a) 10(lf) 98(b) 即可。 13(cr)字符丢失。

我搜索了很多,但没有找到原因。任何解释?非常感谢。


我发现了这个:

https://bugzilla.mozilla.org/show_bug.cgi?id=188015

有人在评论28中回复,官方文档转移到:

http://www.w3.org/TR/html5/syntax.html#preprocessing-the-input-stream

"CR" (U+000D) characters and "LF" (U+000A) characters are treated specially. All CR characters must be converted to LF characters, and any LF characters that immediately follow a CR character must be ignored.

希望能提供帮助。

1 个答案:

答案 0 :(得分:1)

你的html得到的结果是这样的(我已经逃脱了cr角色:)。

<input id="test" type="hidden" value="a\13
b">

并且浏览器修剪空格,从而删除CR字符。

你可以做的是渲染urlencode($ str)而不是普通的$ str变量。这样浏览器就不会弄乱您的控制角色。但是,你必须在另一方面进行urldecode。