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
Share on other sites

Not sure if that's the problem, but remember that < and > will mess up your HTML if not presented as HTML entities, as the browser tries to parse the whole lot as a HTML tag. Could be the reason you're not seeing anything when trying to output it.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.