views:

262

answers:

4

I am templatizing my php.ini using PHP. I have a script to set up a development environment by generating httpd.conf, apachectl, and php.ini from templates using a CLI PHP script. Unfortunately there are literal <? and <?php strings in php.ini (in a comment). Is it possible to escape those somehow so php doesn't interpret them as normal PHP escape sequences?

Currently my workaround is to wrap them in a real PHP escape sequence that outputs them as a string, like this:

; This directive determines whether or not PHP will recognize code between
; <?php echo "<? and ?>" ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php echo "<?php and ?>" ?> tag combination. With the wide spread use
;
A: 

This:

<?php echo "<?php"; ?>

Will output:

<?php
gahooa
A: 

A quick & simple solution would to search through the php.ini and replace all within comments with unique markers, after the file has been parsed, simply replace those unique markers back with the corresponding php tag.

Ben Rowe
+1  A: 

You are asking how you can embed interpreted <?php ... ?> tags in a file but leave <?php ... ?> tags untouched.

I can't really see how that's possible I'm afraid, your current solution looks good.

Alex Barrett
A: 

You could wrap the whole ini inside EOF type thingy which I don't know the name of as follows

<?php 
echo <<< EOF
<?php echo "stuff inside php tag"; ?>
EOF;
?>

Which when output doesn't render in the page but appears on the page source
or you can use html encoding for the tags

&lt;?php echo 'this'; ?&gt;

I think the later will suit you better.
If that doesn't do the job you could use ASCII encoding which I don't know much about

andho
"EOF type thingy which I don't know the name of" -> "Heredoc"
Charles
HTML encoding would only work if the output were HTML. It's not—it's a php.ini file
nohat
Yes I thought so.
andho