运行 json_encode 后替换 \r\n (换行符)
因此,当我运行 json_encode 时,它也会从 MySQL 获取 \r\n 。我尝试重写数据库中的字符串但无济于事。我尝试将 MySQL 中的编码从默认的 latin1_swedish_ci 更改为 ascii_bin 和 utf8_bin。我已经做了很多 str_replace 和 chr(10), chr(13) 的事情。我不知道还能说什么或做什么,所以我就把它留在这里......
$json = json_encode($new);
if(isset($_GET['pretty'])) {
echo str_replace("\/", "/", jsonReadable(parse($json)));
} else {
$json = str_replace("\/", "/", $json);
echo parse($json);
}
jsonReadable 函数来自 这里 解析函数来自 此处。已经存在的 str_replaces 是因为我收到了奇怪格式的 html 标签,例如 。最后,$new 是一个上面制作的数组。根据要求提供完整代码。
帮助我 StackOverflow。你是我唯一的希望
So when I run json_encode, it grabs the \r\n from MySQL aswell. I have tried rewriting strings in the database to no avail. I have tried changing the encoding in MySQL from the default latin1_swedish_ci to ascii_bin and utf8_bin. I have done tons of str_replace and chr(10), chr(13) stuff. I don't know what else to say or do so I'm gonna just leave this here....
$json = json_encode($new);
if(isset($_GET['pretty'])) {
echo str_replace("\/", "/", jsonReadable(parse($json)));
} else {
$json = str_replace("\/", "/", $json);
echo parse($json);
}
The jsonReadable function is from here and the parse function is from here. The str_replaces that are already in there are because I am getting weird formatted html tags like </h1>. Finally, $new is an array which is crafted above. Full code upon request.
Help me StackOverflow. You're my only hope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该字符串是否包含“\r\n”(如 0x0D 0x0A)或文字字符串“\r\n”?如果是前者,这应该删除所有换行符。
(可选)将第二个参数“”替换为“
”如果您想用 br 标记替换换行符。对于后一种情况,请尝试以下操作:
Does the string contain "\r\n" (as in 0x0D 0x0A) or the literal string '\r\n'? If it's the former, this should remove any newlines.
Optionally, replace the second parameter "" with "<br />" if you'd like to replace the newlines with a br tag. For the latter case, try the following:
不要在 JSON 中替换它,而是在编码之前在源中替换它。
Don't replace it in the JSON, replace it in the source before you encode it.
我遇到了类似的问题,我使用:
不确定这是否有助于满足您的要求。
I had a similar issue, i used:
Not sure if this will help with your requirements.