从Laravel中的JSON字符串中删除添加的反斜杠
So I'm trying to store some HTML code in the database and then retrieving it througth the API.
Initial HTML:
<img src="https://www.google.se/images/google_80w1ht.gif" alt="Google logo">
Then I store in the DB without any further hassle:
$data = $request->only(['content');
$comment = $this->repo->update($data, $id);
If I go to MySQL Workbench, this is the data stored:
<img src="https://www.google.se/images/google_80wht.gif" alt="Google logo">
However, the moment I retrieve it want want to output the HTML as a JSON parameter, it get's slahed all over the face.
This is the json:
{"title": "<img src=\"https://www.google.se/images/google_80w1ht.gif\" alt=\"Google logo\">"}
I have not applied any transformation nor during insert nor on retrieval.
How can I remove the added extra backslashes? ()
所以我试图在数据库中存储一些HTML代码,然后通过API检索它。 p >
初始HTML: p>
&lt; img src =“https://www.google.se/images/google_80w1ht.gif”alt =“ Google徽标“&gt;
code> pre>
然后我毫不费力地存储在数据库中: p>
$ data = $ request-&gt; only(['content');
$ comment = $ this-&gt; repo-&gt; update($ data,$ id);
code> pre>
如果我去MySQL Workbench,这是存储的数据: p>
&lt; img src =“https://www.google.se/images/ google_80wht.gif“alt =”Google徽标“&gt;
code> pre>
但是,我检索它的那一刻想要输出HTML作为JSON参数,它会得到 这是json: p>
{“title”:“&lt; img src = \”https: //www.google.se/images/google_80w1ht.gif \“alt = \”Google徽标\“&gt;”}
code> pre>
我没有应用任何转化 插入时也不插入 ieval。 p>
如何删除添加的额外反斜杠? () p>
div>
Try json_decode(your_data)
this should work.
I have not applied any transformation nor during insert nor on retrieval.
You converted it to JSON. That is a transformation.
it get's slahed
The slashes are needed.
Without them this happens:
"<img src="h
^ ^^
| ||
| |Error
| End of string
Start of string
The data doesn't contain the slashes, just the JSON representation of it. When you parse the JSON the escape sequences will be consumed and you will get your original string back.