json_encode和json_decode到textarea-换行问题

时间:2018-09-19 11:36:50

标签: json textarea

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

class YourViewController: UIViewController {
    var sections = [SectionModel]()

    func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].yourTableRowModels.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let rowModel = sections[indexPath.section].yourTableRowModels[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "dataCell") as! DataCell
        cell.chapterName.text = rowModel.someAttribute
        cell.numberOfDocumentsAndAssignments.text = rowModel.someAttribute
        return cell
    }
}

我将值插入数据库,如下所示:

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

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

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

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

1\r\n2

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

我该怎么办?

1 个答案:

答案 0 :(得分:0)

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

accent

然后使用新值对json进行解码

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