dongzhi4239
2017-11-07 10:04
浏览 624
已采纳

从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? ()

图片转代码服务由CSDN问答提供 功能建议

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

初始HTML:

 &lt; img src =“https://www.google.se/images/google_80w1ht.gif”alt =“  Google徽标“&gt; 
   
 
 

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

  $ data =  $ request-&gt; only(['content'); 
 
 $ comment = $ this-&gt; repo-&gt; update($ data,$ id); 
   
  
 

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

 &lt; img src =“https://www.google.se/images/  google_80wht.gif“alt =”Google徽标“&gt; 
   
 
 

但是,我检索它的那一刻想要输出HTML作为JSON参数,它会得到 这是json:

  {“title”:“&lt; img src = \”https:  //www.google.se/images/google_80w1ht.gif \“alt = \”Google徽标\“&gt;”} 
   
 
 

我没有应用任何转化 插入时也不插入 ieval。

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtun2572 2017-11-07 10:13
    已采纳

    Try json_decode(your_data) this should work.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongle3217 2017-11-07 10:16

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题