有这样一个需求,在函数中能读入另外一个PHP文件运行后的结果并做为字符串返回,如:function read_php()
{
     return  file_get_contents("abc.php"); // 使用file_get_contents函数达不到希望的功能
}
但使用file_get_contents函数能将文件返回成字符串,但此文件中的php代码并不执行,而是忽略。使用这个函数的目的是希望实现include的效果,但如果使用include,则将在函数定义的地方发生包含,得不到希望的字符串。希望能得到PHP执行后的结果字符串并将其返回,请问高手有没有办法?

解决方案 »

  1.   

    你用include不就可以执行了吗?
      

  2.   

    file_get_contents("http://xxx.xxx.xxx/xxx.php");
      

  3.   

    你在abc.php里放一个全局变量,然后将执行结果放进这个变量,include它,你的页面就可以使用这个变量了
      

  4.   

    a.php
    <?php
    $tmpReturn = "";
    doInit();
    function doInit(){
    global $tmpReturn;
    include_once("b.php");
    doParse();
    echo $tmpReturn;
    }
    ?> 
    b.php<?php
    function doParse(){
    global $tmpReturn;
    $tmpReturn = "Test";
    }?> 
      

  5.   


    function read_php()
    {
         ob_start();
         include("abc.php");
         return ob_get_clean();
    }
      

  6.   

    用缓冲区 +  include("abc.php");   可以取得  一般静态页用这个