douju9847 2014-11-30 17:06
浏览 124
已采纳

强制file_get_contents()不要将php代码呈现为纯文本

How can I get expected output from example below?

Note: I'm using $content = file_get_contents('content.php'); to use content where and when possible so it is not a direct output on screen. include() breaks the pages.

content.php

<p>Hello <?php echo 'World!'; ?></p>

reader.php

<b>Message from another file:</b> <?php echo file_get_contents('content.php'); ?>

Output of code above is:

Message from another file: Hello <?php echo 'World!'; ?>

Instead of (expected):

Message from another file: Hello World!
  • 写回答

2条回答 默认 最新

  • douzhi0107 2014-11-30 17:11
    关注

    I think you are looking for <?php include('content.php');

    file_get_contents — Reads entire file into a string

    PHP.net file_get_contents - manual

    The include statement includes and evaluates the specified file.

    PHP.net include - manual

    Try making content.php into a file that has a function that returns the content you want (you may want to have parameters). Simply require the file then call the function and save the output.

    Example:

    content.php

    function get_content($world){
      return '<p>Hello ' . $world . '</p>';
    }
    

    reader.php

    <?php 
    require('content.php');
    $content = get_content('world');
    ?>
    <b>Message from another file:</b> <?php echo $content; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条