1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved str_replace JSON files

Discussion in 'Development' started by AppleDevelops, Oct 1, 2017.

  1. 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 $playerint $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
     
    Last edited by a moderator: Oct 1, 2017
  2. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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);
     
    AppleDevelops likes this.
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Seems your JSON is missing a pair of curly braces surrounding the whole thing.
     
    InspectorGadget likes this.
  4. It isn’t in my actual code, I just made an example.
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.