dql7588 2017-03-01 10:05
浏览 39
已采纳

如何在一个文本文件中嵌入PHP以便以后包含?

I have a simple template created through my ClassPage. The class gets called and reads html code from a text file. Inside the navigation I want to echo a PHP variable but all I am getting is the embedded php commented out on the browser's console? I'm thinking perhaps the file that gets read isn't being processed by the server and that is why the php isn't being processed? Or I'm missing something really simple?

Heres the code: ClassPage -> getBody which gets the file called "superNav.txt". The string that gets returned is put together with the needed HTML head tags etc then outputted.

private function getBody() {
    $text = "";
    if ($this->specialUser) {
        $text .= file_get_contents("Template/superNav.txt");
    } else {
        $text .= file_get_contents("Template/userNav.txt");
    }
    return $text;
}

This is the Text File:

<body>
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span id="glyph" class="glyphicon glyphicon-th-large"></span></button> 
            <a class="navbar-brand" href="#"><img alt="brand" id="brandLogo" src="Images/logo.png"></a> 
            <p id="navbarText" class="navbar-text">Webmin</p>
        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li><a href="welcome-super.php">Home</a></li>
                <li><a href="view-rota.php">Rota</a></li>
                <li><a href="rota-administration.php">Rota Admin</a></li>
                <li><a href="user-administration.html">User Admin</a></li>
                <li><a href="rota-archive.php">Archive</a></li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="my-account.php"><span class="glyphicon glyphicon-user"></span><?php echo $user->getName(); ?></a></li>
                <li><a href="#"><span class="glyphicon glyphicon-log-out"></span> Log Out</a></li>
            </ul>
        </div>
    </div>
</nav>

This is the line that is giving me an issue:

<li><a href="my-account.php"><span class="glyphicon glyphicon-user"></span><?php echo $user->getName(); ?></a></li>

The php does not get shown to screen, the browsers console shows that the code is commented.

Any help is welcomed. Thanks in advance!

  • 写回答

2条回答 默认 最新

  • douzhan1868 2017-03-01 10:15
    关注

    For this first get the content from the .txt file and then do something like this :

     $name = $user->getName();
     $text = file_get_contents("Template/superNav.txt");
     $text = str_replace('USERNAME', $name, $message);
     $text$message = str_replace('logo_url', $base_url, $message);
    

    and in your .txt file replace the php value with some Constants.

    like this

    <li><a href="my-account.php"><span class="glyphicon glyphicon-user"></span>USERNAME</a></li>
    

    So here you can see we have replaced the value of USERNAME with dynamic value. Hope it helps!!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮