0

I'm trying to replace the value of model within this json file. The json file contains:

{
    "model": {
        "template": "model.txt",
        "directory": "app/models",
        "filename": "{{Entity}}.php"
    },

I'm trying to use the following regex to replace it and am not able to get it to match...

$json = preg_replace('/"model": \{(.*?)\},/i', '"model": false', $json);

I recognize i can change this value after it has been decoded and that is the ideal way to handle this. For various reasons, running this regex would be the simplest way to handle this specific solution.

Here's what I'm working with:

        $json = file_get_contents($path);
        $json = preg_replace('/"model": \{(.*?)\},/i', '"model": false', $json);
var_dump($json);
        exit;

2 Answers 2

1

These should work:

The first pattern will not limit the number of items within the "model":

🔗 preg_replace('/("model.*\{.*(\n.*)+?\n.+\},)/', '"model": false', $json);

This will limit the match specifically to the number of lines in your example:

🔗 preg_replace('/("model.*\{.*(\n.*){3}\n.+\},)/', '"model": false', $json);

Sign up to request clarification or add additional context in comments.

3 Comments

Neither of those are finding a match for me.
Did you look at the examples I linked? Maybe you need additional escape characters within your pattern.
Doh! I didn't. I clearly had pasted something incorrectly because it works now. Thank you!
0

given $json the original json content

$ajson=json_decode($json);
$ajson['model']=false;
$json=json_encode($ajson);

1 Comment

Doh, sorry. I should've specified I'm looking for PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.