将 php 文件加载到变量中时出现问题(加载 php 代码的结果而不是代码作为字符串)

发布于 2024-12-01 16:14:59 字数 1389 浏览 3 评论 0原文

我有一个网站架构,我将内容分配给变量,然后在母版页中打印它们。我的问题是子页面中的 php 代码作为字符串导入到变量中。是否有办法确保代码实际执行并将结果导入到变量中?

在下面的示例中,signup_header.php 中的 php 代码作为字符串导入到 $page_header。结果是“getVerifiedEmail(); ?>”显示在表单元素中而不是电子邮件地址中。

master.phpsignup.php

<!DOCTYPE HTML>
<html>
<head>
    <?php echo $page_header; ?>
</head>

<body id="home">
    <div class = "container">
       <?php echo $page_content; ?>
    </div>
</body>
</html>

<?php
    $page_content = file_get_contents("./include/signup_content.php");
    $page_header = file_get_contents("./include/signup_header.php");
    include('master.php');
?>

signup_header.phpsignup_content.php

<script type="text/javascript">
   $(document).ready(function(){
   $('input[name="name"]').attr('value', "<?php echo $idpAssertion->getVerifiedEmail(); ?>");
    });        
</script>

<section>
    <form class="task" method="POST">
        Name: <input type="text" name="name" maxlength="30" value=""/><br/>
        Email: <input type="text" name="email" value=""/><br/>
        UserId: <input id="userId" type="text" name="userId" value="" /><br/>
    </form>
</section>

I have a site architechure where I assign content to variables and then print them in a master page. My problem is that php code in the sub pages is imported into the variables as strings. Is there anyway to make sure that the code is actually executed and the results is imported in the variables instead?

In the example below the php code in signup_header.php is imorted as a string to $page_header. The result is that "getVerifiedEmail(); ?>" is displayed in the form element instead of the e-mail address.

master.php

<!DOCTYPE HTML>
<html>
<head>
    <?php echo $page_header; ?>
</head>

<body id="home">
    <div class = "container">
       <?php echo $page_content; ?>
    </div>
</body>
</html>

signup.php:

<?php
    $page_content = file_get_contents("./include/signup_content.php");
    $page_header = file_get_contents("./include/signup_header.php");
    include('master.php');
?>

signup_header.php

<script type="text/javascript">
   $(document).ready(function(){
   $('input[name="name"]').attr('value', "<?php echo $idpAssertion->getVerifiedEmail(); ?>");
    });        
</script>

signup_content.php

<section>
    <form class="task" method="POST">
        Name: <input type="text" name="name" maxlength="30" value=""/><br/>
        Email: <input type="text" name="email" value=""/><br/>
        UserId: <input id="userId" type="text" name="userId" value="" /><br/>
    </form>
</section>

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

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

发布评论

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

评论(5

梅窗月明清似水 2024-12-08 16:14:59

使用 file_get_contents文档将返回实际文件的内容。但您希望执行该文件。您可以使用include文档 执行一个 php 文件,但是大多数情况下该文件本身已经创建了输出。这可能不是您想要的。

相反,您仍然可以使用 include 但将输出捕获到缓冲区中。这称为输出缓冲 文档

为了使您的程序更容易访问此内容,您可以创建一个处理详细信息的小辅助函数。然后,您可以调用该函数来包含相关文件并返回实际输出。然后您可以将返回值分配给变量。

示例:

<?php
    /**
     * include_get_contents
     *
     * include a file and return it's output
     *
     * @param string $path filename of include
     * @return string
     */
    function include_get_contents($path)
    {
        ob_start();
        include($path);
        return ob_get_clean();
    }

    $page_content = include_get_contents("./include/signup_content.php");
    $page_header = include_get_contents("./include/signup_header.php");
    include('master.php');
?>

相关:修改现有 PHP 函数以返回字符串的答案< /a>

Using file_get_contentsDocs will return the actual file's content. But you're looking to execute the file instead. You can use includeDocs to execute a php file, however most often that file will create output itself already. This is probably not what you want.

Instead, you can still use include but catch the output into a buffer. This is called output-buffering Docs.

To make this more accessible for your program, you can create a small helper function that deals with the details. You can then just call that function that will include the file in question and return the actual output. You can then assign the return value to your variables.

Example:

<?php
    /**
     * include_get_contents
     *
     * include a file and return it's output
     *
     * @param string $path filename of include
     * @return string
     */
    function include_get_contents($path)
    {
        ob_start();
        include($path);
        return ob_get_clean();
    }

    $page_content = include_get_contents("./include/signup_content.php");
    $page_header = include_get_contents("./include/signup_header.php");
    include('master.php');
?>

Related: Answer to Modify an Existing PHP Function to Return a String

顾挽 2024-12-08 16:14:59

file_get_contents 返回文件的实际内容,您需要的是 < code>include,实际上是解析一个PHP文件。

file_get_contents returns the actual contents of a file, what you need is include, which actually parses a PHP file.

浅语花开 2024-12-08 16:14:59
<?php
    $page_content = "./include/signup_content.php";
    $page_header = "./include/signup_header.php";
    include('master.php');
?>

<!DOCTYPE HTML>
<html>
<head>
    <?php include $page_header; ?>
</head>

<body id="home">
    <div class = "container">
       <?php include $page_content; ?>
    </div>
</body>
</html>

就是

我希望的signup_content.php 仅包含类似的模板

<?php
    $page_content = "./include/signup_content.php";
    $page_header = "./include/signup_header.php";
    include('master.php');
?>

and

<!DOCTYPE HTML>
<html>
<head>
    <?php include $page_header; ?>
</head>

<body id="home">
    <div class = "container">
       <?php include $page_content; ?>
    </div>
</body>
</html>

that's all

I hope that signup_content.php contains the similar template only

柒夜笙歌凉 2024-12-08 16:14:59

在signup.php中使用

<?php
$page_content = include("./include/signup_content.php");
$page_header = include("./include/signup_header.php");
include('master.php');
?>

这就是你所需要的。

in signup.php use

<?php
$page_content = include("./include/signup_content.php");
$page_header = include("./include/signup_header.php");
include('master.php');
?>

that is what you need.

趁年轻赶紧闹 2024-12-08 16:14:59

使用可以使用eval函数

http://php.net/manual/en/function.eval。 php

$string = eval('?'.'>'.file_get_contents('signup_content.php',1).'<'.'?');
echo $string;

use can use eval function

http://php.net/manual/en/function.eval.php

$string = eval('?'.'>'.file_get_contents('signup_content.php',1).'<'.'?');
echo $string;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文