Without resorting to using regex, is there a way to do so if I saved my array into json format? I'm interesting in json only because I'm using mongodb so the output comes out in json format. I have a field called docroot
which is essentially a directory path.
docroot : "secure.unstable.qa.example.com"
The only two pieces that could change depending on other factors are unstable
and qa.
What I'm hoping for is a way to place "markers" so that they could easily be replaced with an appropriate variable.
For example:
docroot : "secure.{STREAMS}.{ENV}.example.com"
docroot : "unsecure.{STREAMS}.{ENV}.example.com"
If the variables are guaranteed to be in some specific order (they are, according to your example), then I would advise not reinventing the wheel:
// Assuming $json contains your JSON
$streams = 'foo';
$env = 'bar';
$data = json_decode($json, TRUE);
var_dump($data['docroot']); // "secure.%s.%s.example.com"
var_dump(sprintf($data['docroot'], $streams, $env)); // "secure.foo.bar.example.com"
If the variables are not guaranteed, or you'd just like to use more descriptive placeholders, just pick some unique delimiters (some character or set of characters that is unlikely to actually appear in your data), and use str_replace()
.