How would I go about using str_replace to replace variables for my JSON files? My code for using the json stuff: PHP: public function addModal(Player $player, int $id){ $config = (new Config("my/file/path/modals.json"))->getAll(); $pk = new ModalFormRequestPacket(); $pk->formId = $id; $pk->formData = json_encode($config[$id]); $player->dataPacket($pk);} Example JSON modal: Code: "1": { "type": "modal", "title": "{player}", "content": "Example", "button1": "Close", "button2": "§cClose but in red" } EDIT: Jack got part of it working for me, but anything it doesn't replace for the array in the json data. EDIT EDIT: Fixed, I'm just retarded, thanks @Jack Noordhuis
Why bother using a config if you're just calling Config::getAll()? Just use file_get_contents() directly. The same as you would with any other string in php: PHP: $pk = new pocketmime\network\mcpe\protocol\ModalFormRequestPacket();$pk->formId = $id;$pk->formData = str_replace("{player}", $player->getName(), json_encode(json_decode(file_get_contents("your/file/path/models.json"), true)[$id]));$player->dataPacket($pk);
This is most likely vulnerable to injection, because if the data do not come entirely from the config (but for example, partly from player input), it may replace those parts too. Consider using array_map() instead, or simply foreach the array and replace each value as PHP native data.