Edit php code and variables in file -


i have php file comments , code.

<?php  // comments define('thing', 'thing1');  $variable = 'string1';  if ($statement === true) { echo 'true1'; } 

i want know best way edit file change variables , spit out new version of file changes.

<?php  // comments define('thing', 'thing2');  $variable = 'string2';  if ($statement === true) { echo 'true2'; } 

the file large. write function adds massive string output escaping have comments etc headache.

i thinking of including file allow it's variables used within class.

so far thing can think of make 'skeleton' version of file (below) variables want change written file. can assign them, dumping out file either of above examples has escaped me.

best way of doing this?

<?php  // comments define('thing', $thing);  $variable = $string;  if ($statement === true) { echo $true; } 

i going echo @prisoner's comment above, see mentioned working limitation. can use strtr() basic templating, so:

<?php  $template = <<<'str' hi there {{ name }}  it's {{ day }}. how today?  str;  $vars = [     '{{ name }}' => 'darragh',     '{{ day }}'  => date('l'), ];  $result = strtr($template, $vars); 

which yields following string:

"hi there darragh  it's monday. how today?" 

you can write result file, echo etc.

for specific example above:

<?php  $template = <<<'str' <?php  define('thing', '{{ const }}');  $variable = '{{ variable }}';  if ($statement === true) { echo '{{ echo }}'; } str;  $vars = [     '{{ const }}'    => 'thing1',     '{{ variable }}' => 'string1',     '{{ echo }}'     => 'true1', ];  echo $result = strtr($template, $vars); 

yields:

"<?php  define('thing', 'thing1');  $variable = 'string1';  if ($statement === true) { echo 'true1'; }" 

hope helps :)


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -