从 Laravel 中的 JSON 字符串中删除添加的反斜杠

Remove added backslashes from JSON string in Laravel

所以我试图在数据库中存储一些 HTML 代码,然后通过 API.

检索它

初始HTML:

<img src="https://www.google.se/images/google_80w1ht.gif" alt="Google logo">

然后我毫不费力地存储在数据库中:

$data = $request->only(['content');

$comment = $this->repo->update($data, $id);

如果我转到MySQL Workbench,这是存储的数据:

<img src="https://www.google.se/images/google_80wht.gif" alt="Google logo">

然而,当我检索它想要将 HTML 作为 JSON 参数输出时,它被打得满脸都是。

这是json:

{"title": "<img src=\"https://www.google.se/images/google_80w1ht.gif\" alt=\"Google logo\">"}

我没有应用任何转换,也没有在插入期间或检索时应用。

如何删除添加的额外反斜杠? ()

尝试 json_decode(your_data) 这应该有效。

I have not applied any transformation nor during insert nor on retrieval.

您将其转换为 JSON。那就是变身。

it get's slahed

需要斜线。

如果没有他们,就会发生这种情况:

"<img src="h
^         ^^
|         ||
|         |Error
|         End of string
Start of string

数据 不包含斜杠,只包含它的 JSON 表示。当您解析 JSON 时,转义序列将被消耗,您将取回原始字符串。