删除周围的引号json文件,仅限数字,php preg_replace

时间:2016-12-21 20:04:19

标签: php json numbers preg-replace double-quotes

使用C语言或其他语言发现一些解决方案,但在php中没有用。

我需要替换(大)json文件中的所有数字,以避免在javascript中使用时将数字视为字符串。

例如:

[["Alt","128","36.00","36.00","test" .....]]

我想要的是什么:

[["Alt",128,36.00,36.00,"test" .....]]

尝试过几件事,但我不是preg专家,这样的事情不起作用:

$sOutput = preg_replace('/^(\'[0-9]\'|"([0-9])")$/', '$2$3', $sOutput );
die( $sOutput );

我如何实现目标?

1 个答案:

答案 0 :(得分:1)

$re = '/\"([0-9.]+)\"/m';
$str = '[["Alt","128.12","36.00","36.00","test" ....., "123.45"]]';
$subst = '$1';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;