json中的str_replace值如何?

吉里·K。

有人可以帮我在json文件中提供str_replace值吗?我的代码在替换字符串上运行良好,但是在json中的值上却不起作用。

在所有json文件中都能很好地将“旧”正确替换为“新”:

foreach(glob('*.json') as $path_to_file) {
    $file_contents = file_get_contents($path_to_file); 
    $file_contents = str_replace('old','new',$file_contents);
    file_put_contents($path_to_file,$file_contents);
}

但是当我需要替换时"min_order":"""min_order":"1"它不起作用。我不能直接替换"""1",因为json中有许多其他值。

我测试了这段代码,但是没有用:

foreach(glob('*.json') as $path_to_file) {
    $file_contents = file_get_contents($path_to_file);
    $file_contents = str_replace('"min_order":""','"min_order":"1"',$file_contents);
    file_put_contents($path_to_file,$file_contents);
}

有人可以帮我解决这个问题吗?

先感谢您。吉里

罗恩

一旦你做 $file_contents = file_get_contents($path_to_file);

然后,您应该使用json_decode($file_contents);将json字符串转换为数组或对象,

然后通过替换需要替换的键的值来操纵数组/对象,

然后在修改后的数组上使用json_encode();以将数组/对象转换回JSON格式

最后 file_put_contents($path_to_file,$file_contents);

在JSON中可以在json格式上使用str_replace,但不建议这样做,也不可行。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

str_replace NULL 值

来自分类Dev

DOM中的str_replace()

来自分类Dev

如何在PHP中str_replace \符号

来自分类Dev

PHP str_replace不替换值

来自分类Dev

PHP str_replace不替换值

来自分类Dev

str_replace函数如何工作?

来自分类Dev

如何使用多个str_replace?

来自分类Dev

str_replace函数如何工作?

来自分类Dev

替换str_replace中的整数

来自分类Dev

是str_replace中的错误吗?

来自分类Dev

为什么str_replace函数用文件中的错误值替换该值?

来自分类Dev

如何用str_replace将csv文件中的php中的单引号替换为2?

来自分类Dev

在PHP中结合substr_replace和str_replace

来自分类Dev

如何使用str_replace替换PHP中的多个字符串?

来自分类Dev

如何在str_replace()的search参数的数组中获取数值以映射期望的方式?

来自分类Dev

如何在single_post_title中插入str_replace?

来自分类Dev

如何使用str_replace在PHP字符串中添加双引号字符

来自分类Dev

如何在str_replace()的搜索参数的数组中获取数值以映射期望的方式?

来自分类Dev

如何使用str_replace替换PHP中的多个字符串?

来自分类Dev

PHP使用str_replace替换多个值?

来自分类Dev

PHP str_replace with array() 嵌套覆盖替换值

来自分类Dev

str_replace 以 x 开头并以 x 结尾的值

来自分类Dev

str_replace 函数无法替换多个值

来自分类Dev

如何在Laravel中使用str_replace

来自分类Dev

如何在Smarty中使用str_replace

来自分类Dev

如何str_replace Facebook共享的Google新闻RSS?

来自分类Dev

如何检测str_replace()是否已完成替换?多少?

来自分类Dev

如何在多维数组上使用 str_replace?

来自分类Dev

如何使用 str_replace() 函数编辑永久链接

Related 相关文章

热门标签

归档