【问题标题】:why CRLF transformed to LF when they are assigned to <input> using php为什么 CRLF 在使用 php 分配给 <input> 时转换为 LF
【发布时间】:2015-06-10 02:38:10
【问题描述】:

(1)表单包含textarea,输入带有换行符的文本。Like :

一个
b

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

(2)使用smarty.Like获取内容并赋值给模板:

//$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)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.

希望能提供帮助。

【问题讨论】:

  • 为什么要将多行文本存储到单行编辑控件中?
  • @Cristik 我调查了之前写的代码。 用作媒介,其他一些代码需要使用这个 来获取值。从这里开始,长度是错误的。
  • @AnoccLinn 我认为@Cristik 的意思是,如果您的数据有多行,则应该使用&lt;textarea&gt;
  • @T0xicCode 实际上使用了&lt;textarea&gt;。但是实现是使用&lt;input&gt;的值设置&lt;textarea&gt;的值。&lt;input&gt;的值是错误的,所以是&lt;textarea&gt;.

标签: javascript php html dom


【解决方案1】:

发生的情况是你的 html get 是这样渲染的(我已经转义了 cr 字符:)

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

浏览器会修剪空白,从而删除 CR 字符。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多