动态修改JSON属性文本

时间:2015-04-22 21:55:57

标签: php json

我有一个JSON数组,我想用PHP动态修改,但不知道如何。在网上搜索,但找不到我需要的东西。 这是我的代码

eq

提前致谢。 编辑:其实我使用FabricJS JavaScript库进行画布操作。上面的代码表示在画布上绘制内容的画布代码。我必须动态修改文本。

1 个答案:

答案 0 :(得分:1)

此解决方案处理的事实是给定的JSON字符串有效(companyName未包含在字符串中),这就是为什么在这种特殊情况下,不能使用{ {1}}:

json_decode()

更新(在Badar修复了JSON之后)

最快的解决方案是:

$string = '{"objects":[{"type":"text","originX":"left","originY":"top","left":46.05,"top":129.71,"width":412.09,"height":52,"fill":"rgb(0,0,0)","stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1.33,"scaleY":1.33,"angle":352.36,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"backgroundColor":"red","fillRule":"nonzero","globalCompositeOperation":"source-over","text":companyName,"fontSize":40,"fontWeight":"normal","fontFamily":"Delicious_500","fontStyle":"","lineHeight":1.3,"textDecoration":"","textAlign":"left","path":null,"textBackgroundColor":"","useNative":true}],"background":""}';

$newString  = preg_replace('/("text"):(companyName)/', '${1}:"Acme Ltd."', $string);

// To check if the string is now valid JSON, will return a PHP array
print_r(json_decode($newString, true));