首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我们在php中json_encode数组时,我们如何防止添加特殊字符?

当我们在php中json_encode数组时,我们如何防止添加特殊字符?
EN

Stack Overflow用户
提问于 2022-01-12 10:03:44
回答 1查看 32关注 0票数 0

主要数组是:

代码语言:javascript
复制
array (size=2)
  0 => 
    array (size=2)
      'message' => string 'Dear subscriber, 
We are working to resolve your link down issue. 
It will take approximately 2-4 hours to fix the issue.We appreciate your cordial support and patience during this time.

Thanks for being with us.
Techno' (length=231)
      'created_at' => string '01:04 pm 11/01/2022' (length=19)
  1 => 
    array (size=2)
      'message' => string 'Dear subscriber, 
We are working to resolve your link down issue. 
It will take approximately 2-4 hours to fix the issue.We appreciate your cordial support and patience during this time.

Thanks for being with us.
Techno' (length=231)
      'created_at' => string '01:04 pm 11/01/2022' (length=19)

在json_encode之后,集合会变成这样(其中包含了我们不想要的额外\n \r ):

代码语言:javascript
复制
'[{"message":"Dear subscriber, \nWe are working to resolve your link down issue. \nIt will take approximately 2-4 hours to fix the issue.We appreciate your cordial support and patience during this time.\n\nThanks for being with us.\nTechno","created_at":"01:04 pm 11\/01\/2022"},{"message":"Dear subscriber, \nWe are working to resolve your link down issue. \nIt will take approximately 2-4 hours to fix the issue.We appreciate your cordial support and patience during this time.\n\nThanks for'... (length=1173) 
EN

回答 1

Stack Overflow用户

发布于 2022-01-12 11:03:56

如果您所需要的只是删除您提到的这两个字符(基本上是新行),您可以在json_encode $result上尝试这样的操作:

代码语言:javascript
复制
$result = str_replace("\\n", '', $result);

$result = str_replace("\\r", '', $result);

例如,您可以找到更多的here选项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70679621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档