简体   繁体   English

用JSON字符串中的空字符串替换制表符和换行符

[英]Replace tab & new line feed with empty string in JSON string

I have a JSON string which is like this when I log it: 我有一个JSON字符串,它在登录时是这样的:

..."items":{"0":"
\t\t\t\t\t"},"buys":{"0":"
\t\t\t\t\t"}}}, "sells":{}, "clients":{"test":{"0":"
\t\t\t\t"}...

What I want is to delete all new line feed (\\n) & tab characters(\\t) from it and have a JSON string like this : 我想要从中删除所有new line feed (\\n) & tab characters(\\t) ,并具有以下JSON字符串:

..."items":{},"buys":{}}}, "sells":{}, "clients":{"test":{}...

I have written a preg_replace function like this : 我写了这样的preg_replace函数:

$json = preg_replace(['/\"0\"\:\n\"[\\t]+/'], [''], $json);

but When I log the JSON string they have not been deleted . 但是当我记录JSON字符串时,它们尚未被删除。

What should I do ? 我该怎么办 ?

You can use str_replace() function to replace the \\t and \\n : 您可以使用str_replace()函数替换\\t\\n

$json = str_replace(array('\t','\n'),'',$json);

You can read more about this here . 您可以在此处了解更多信息。

You may use this regular expression 您可以使用此正则表达式

"[\t\n\r]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM