Reputation: 1460
I have a JSON array, that I want to modify dynamically using PHP, but don't know how to. Searched online, but couldn't find what I needed. Here is my code
{"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":""}
Thanks in advance. EDIT: Actually I am using FabricJS JavaScript library for canvas manipulation. The code above represents a canvas code that draws something onto the canvas. I have to modify the text dynamically.
Upvotes: 0
Views: 59
Reputation: 3551
This solution handles the fact that the given JSON string is not valid (companyName
is not enclosed in strings), that's why in this particular case, it is not possible to use json_decode()
:
$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));
Update (after Badar has fixed the JSON)
The fastest solution would be this:
$json = '{"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":""}';
$data = json_decode($json, true);
$data['objects'][0]['text'] = 'New text';
$newJson = json_encode($data);
echo $newJson;
Upvotes: 1