标签云

微信群

扫码加入我们

WeChat QR Code

I'm trying to echo a PHP tag by doing this:echo "<?php echo \"test\"; ?>";The result should be just "test" without quotes, but my code isn't working. What is happening is that nothing is shown on the page, but the source code is "<?php echo "teste"; ?>"Most of you will want to know why I want to do this. I'm trying to make my own template system; the simplest way is just using file_get_contents and replacing what I want with str_replace and then using echo.The problem is, that in the template file, I have to have some PHP functions that doesn't work when I echo the page, is there another simple way to do this? Or if you just answer my question will help a lot!Here is an example of what I am trying to accomplish:template.tpl:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>[__TITULO__]</title></head><body ><p>Nome: [__NOME__] <br />Email: <a href="mailto:[__EMAIL__]">[__EMAIL__]</a><br /><?phpif ($cidade != "") {?>Cidade: [__CIDADE__]<br /><?php}?>Telefone: ([__DDD__])&nbsp;&nbsp;[__TELEFONE__] <br />Fax:([__DDDFAX__])&nbsp;&nbsp;[__FAX__] <br />Interesse: [__INTERESSE__]<br />Mensagem: [__MENSAGEM__] </p></body></html>index.php<?php$cidade = "Teste";$file = file_get_contents('template.php');$file = str_replace("[__TITULO__]","Esse Título é téste!", $file);$file = str_replace("[__NOME__]","Cárlos", $file);$file = str_replace("[__EMAIL__]","moura.kadu@gmail.com", $file); if ($cidade != "") {$file = str_replace("[__CIDADE__]",$cidade, $file); }echo $file;?>I can solve all this just not showing the div that has no content. like if i have a template, and in it i have 2 divs:<div id="content1">[__content1__]</div><div id="content2">[__content2__]</div>if the time that i set the content to replace the template I set the content1 and not set content 2 the div content2 will not show...


Look up "escaping" in the PHP manual.

2019年04月18日31分31秒

<?php echo "teste"; ?> is malformed HTML, that's why it doesn't appear in the rendered output (it should be in the source though). Are you looking for a template solution?: Twig, Smarty, Dwoo, More.... Trust me - your current idea is bad. OR --- Are you trying to output visible PHP code snippets to the browser?

2019年04月18日31分31秒

its not it, i want the code in echo render as php code as if it was in the source page.

2019年04月18日31分31秒

the result should NOT be just "test" without quotes

2019年04月18日31分31秒