如何回显 PHP 标签?

发布于 2025-01-03 22:34:44 字数 2070 浏览 1 评论 0原文

我试图通过这样做来回显 PHP 标签:

echo "<?php echo \"test\"; ?>";

结果应该只是不带引号的“测试”,但我的代码不起作用。发生的情况是页面上没有显示任何内容,但源代码是 ""

你们中的大多数人都想知道我为什么要这样做这。我正在尝试制作自己的模板系统;最简单的方法就是使用 file_get_contents 并用 str_replace 替换我想要的内容,然后使用 echo

问题是,在模板文件中,我必须有一些 PHP 函数,当我回显页面时,这些函数不起作用,是否有另一种简单的方法来做到这一点?或者如果您回答我的问题将会有很大帮助!

这是我想要完成的示例:

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 />

<?php
if ($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__]","[email protected]", $file); 

if ($cidade != "") {
    $file = str_replace("[__CIDADE__]",$cidade, $file); 
}

echo $file;

?>

我可以解决所有这些问题,只是不显示没有内容的 div 。就像我有一个模板,其中有 2 个 div:

<div id="content1">[__content1__]</div>
<div id="content2">[__content2__]</div>

如果我设置内容来替换模板的时间,我设置了 content1 而不是设置了 content 2,则 div content2 将不会显示...

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 />

<?php
if ($cidade != "") {?>
    Cidade: [__CIDADE__]<br />
    <?php
}
?>

Telefone: ([__DDD__])  [__TELEFONE__] <br />
Fax:
([__DDDFAX__])  [__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__]","[email protected]", $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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2025-01-10 22:34:44

使用 htmlspecialchars

这将转换 >到 <>

Use htmlspecialchars

That will convert the < > to < and >

失与倦" 2025-01-10 22:34:44

您在这里处理的是永远不应该混淆的两组源代码 - 服务器代码(PHP,即 标记中的内容)和客户端(或浏览器) )包含所有 HTML 标签的代码。服务器代码的输出是发送到浏览器的本身代码。在这里,您实际上成功地回显了 PHP 标记,但它对浏览器来说毫无意义,这就是为什么浏览器会忽略它并且不显示任何内容,除非您查看发送到的客户端代码它。

要实现这种风格的模板,要么它们不应该有任何 PHP 代码,要么生成的字符串(存储在 $file 中)本身应该像 PHP 一样执行,而不是回显它直接给客户。有多种方法可以做到这一点。一种是解析字符串中的 PHP 标签,回显 PHP 标签之外的所有内容,并对所有内容运行 eval()

You are dealing with two sets of source code here that should never be confused - the server code (PHP, which is whatever is in the <?php ?> tags) and the client (or browser) code which includes all HTML tags. The output of the server code is itself code that gets sent to the browser. Here you are in fact successfully echoing a PHP tag, but it is meaningless to the browser, which is why the browser ignores it and doesn't show anything unless you look at the client code that got sent to it.

To implement templates in this style, either they should not have any PHP code, or the resulting string (which you have stored in $file) should itself be executed as though it were PHP, rather than echoing it straight to the client. There are various ways to do this. One is to parse out the PHP tags in the string, echo everything that is not within the PHP tags and run eval() on everything that is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文