【问题标题】:Json_encode and json_decode to textarea - line breaks problemJson_encode 和 json_decode 到 textarea - 换行问题
【发布时间】:2018-09-19 11:36:50
【问题描述】:

我在 textarea 中遇到换行问题 我有一个带有 textarea 的表单,如下所示:

<form method="post" action="index.php">
<label><textarea name="content_stream"></textarea>
</form>

我像这样将值插入到数据库中:

$content_streams = $_POST["content_stream"];
$stream = !empty($content_streams) ? json_encode( $content_streams, JSON_UNESCAPED_UNICODE ) : "";
...insert to db...

我打印了 $stream 变量,他是这样的:(因为我在 textarea 中插入了换行符)

1\r\n2

但是当我获取这样的结果时:

$content_streams = $db["content_stream"];
    $stream = !empty($content_streams) ? json_decode( $content_streams, true ) : array();

我得到 json 解析错误,因为换行符的 \r\n。没有换行符它的工作。

我能做什么?

【问题讨论】:

  • 解决方法:$stream = str_replace("\\r\\n", "\\\\n", $stream);

标签: json textarea


【解决方案1】:

如果该值是从 IOS 设备发送的,则在 php 中添加以下行

$json = str_replace("\n","\\n",$json);

然后使用新值进行 json 解码

$json = json_decode($json, true);

【讨论】:

    猜你喜欢
    • 2012-03-08
    • 1970-01-01
    • 2018-11-05
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多