运行 json_encode 后替换 \r\n (换行符)

发布于 2024-10-03 19:24:22 字数 813 浏览 0 评论 0原文

因此,当我运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不乱于心 2024-10-10 19:24:22

该字符串是否包含“\r\n”(如 0x0D 0x0A)或文字字符串“\r\n”?如果是前者,这应该删除所有换行符。

$json = preg_replace("!\r?\n!", "", $json);

(可选)将第二个参数“”替换为“
”如果您想用 br 标记替换换行符。对于后一种情况,请尝试以下操作:

$json = preg_replace('!\\r?\\n!', "", $json);

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.

$json = preg_replace("!\r?\n!", "", $json);

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 = preg_replace('!\\r?\\n!', "", $json);
呆萌少年 2024-10-10 19:24:22

不要在 JSON 中替换它,而是在编码之前在源中替换它。

Don't replace it in the JSON, replace it in the source before you encode it.

生来就爱笑 2024-10-10 19:24:22

我遇到了类似的问题,我使用:

$p_num = trim($this->recp);
$p_num = str_replace("\n", "", $p_num);
$p_num = str_replace("\r", ",", $p_num);
$p_num = str_replace("\n",',', $p_num);
$p_num = rtrim($p_num, "\x00..\x1F");

不确定这是否有助于满足您的要求。

I had a similar issue, i used:

$p_num = trim($this->recp);
$p_num = str_replace("\n", "", $p_num);
$p_num = str_replace("\r", ",", $p_num);
$p_num = str_replace("\n",',', $p_num);
$p_num = rtrim($p_num, "\x00..\x1F");

Not sure if this will help with your requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文