Pezzoni Posted March 18, 2008 Share Posted March 18, 2008 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 More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495368 Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 Actually, this is the problem. I just tried to run your first example, and you're right about the browser not showing the parts, but if I view the source, it's all there. Link to comment https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495371 Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 When I'm testing and need to output something, I often use a textarea to show the data, since my experiences say that that's the only way to show the exact output as is, without having the browser messing it up. Link to comment https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495378 Share on other sites More sharing options...
BlueSkyIS Posted March 18, 2008 Share Posted March 18, 2008 try outputting this string into a text field: <HTML Here is "something's friend, for you<<" unless you use htmlspecialentities(), that will break a form text field. Link to comment https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495379 Share on other sites More sharing options...
thebadbad Posted March 18, 2008 Share Posted March 18, 2008 Oh yeah, don't know what I was thinking. Of course textareas will fail sometimes too. So I guess you have to view the source, to see the actual output untampered with? Link to comment https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495389 Share on other sites More sharing options...
Pezzoni Posted March 18, 2008 Author Share Posted March 18, 2008 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 Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/96800-outputting-php/#findComment-495403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.