为什么当CRLF被分配到<;输入>;使用php


why CRLF transformed to LF when they are assigned to <input> using php

(1)表单包含文本区域,在其中键入带换行符的文本。例如:

a
b

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

(2) 获取内容并使用smarty将其分配给模板。类似:

//$string is get from database.
$smarty->assign('str', $string);

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

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

(3) 获取输入值的长度

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

使用时

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

如果我提交表格并获得子项的ascii码,它们是97(a)10(lf)8(b)。字符13(cr)丢失。

我在谷歌上搜索了很多,但没有找到原因。有什么解释吗?提前非常感谢。


我发现了这个:

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

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

http://www.w3.org/TR/html5/syntax.html#preprocessing-输入流

"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.

希望能帮忙。

发生的情况是,您的html get呈现为这样(我已经转义了cr字符:)

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

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

您可以做的是呈现urlencode($str),而不是简单的$str变量。这样浏览器就不会干扰你的控制字符。不过,你必须在另一边进行url解码。