如何从 JSON 字符串中删除所有不必要的空格(在 PHP 中)?
我假设我需要将 preg_replace 与一些聪明的正则表达式一起使用,以免触及作为值一部分的空格。
一个简单的例子是:
之前: '{ "key": "value with whitespaces to maintain"}'
之后 '{"key":"value with whitespaces to maintain"}'
基本上,我正在寻找一种在不更改任何数据的情况下尽可能压缩和打包字符串的方法。
最佳答案
很抱歉陈述显而易见的事实:
$before = '{ "key": "value with whitespaces to maintain" }';
$after = json_encode(json_decode($before));
它实际上与您的示例完美匹配,请参见$after
:
{"key":"value with whitespaces to maintain"}
关于php - 从 JSON 字符串中删除所有不必要的空格(在 PHP 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859713/