Jump to content

Outputting PHP


Pezzoni

Recommended Posts

Hi,

 

I'm currently in the process of writing myself a PHP Framework, a part of which is a template engine. The exact details aren't important, but one of the things I need to be able to do is to have variables which contain PHP - specifically the opening and closing <?php ?> tags. At the moment, if I have a variable containing these, then the whole section encapsulated by them does not appear - adding a backslash to break the tags (i.e. changing to <\?php ?>) fixes the problem, but is obviously no good for me.

 

As an example, this does not work:

<?php
error_reporting(E_ALL);

$test = 'Hey {$user}, how are {$things[$and]}?';

echo 'Before:<br />';
echo $test;

echo '<br /><br />';

$test = preg_replace('/\{\$([A-Za-z0-9]+|[A-Za-z0-9]+\[\'[A-Za-z0-9]+\'\]|[A-Za-z0-9]+\[\$[A-Za-z0-9]+\])\}/', '<?php echo $$1; ?>', $test);

echo 'After:<br />';
print_r($test);

?>

 

Wheras this does:

<?php
error_reporting(E_ALL);

$test = 'Hey {$user}, how are {$things[$and]}?';

echo 'Before:<br />';
echo $test;

echo '<br /><br />';

$test = preg_replace('/\{\$([A-Za-z0-9]+|[A-Za-z0-9]+\[\'[A-Za-z0-9]+\'\]|[A-Za-z0-9]+\[\$[A-Za-z0-9]+\])\}/', '<\?php echo $$1; ?>', $test);

echo 'After:<br />';
print_r($test);

?>

 

I know the regex isn't quite a perfect description of the PHP variable grammar, but it's good enough for now.

 

Thanks for any help,

Link to comment
https://forums.phpfreaks.com/topic/96800-outputting-php/
Share on other sites

The actual output is designed to be either output by caching it to a executable PHP file, or calling eval() on the variable, so given that it appears that this is actually the browsers doing, and the actual PHP is present in the source, things are good again :D

 

Thanks for the help.

 

Link to comment
https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495403
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.