【问题标题】:Get parsed PHP file获取解析的 PHP 文件
【发布时间】:2012-02-27 14:45:22
【问题描述】:

我试图在解析后获取 PHP 文件的内容,然后将其存储在变量中。 除了这个例子,我无法通过 Google 获得任何有用的信息:

ob_start();
include $file;
$content = ob_get_clean();

但这会将内容返回为纯文本,即:<?php?> 标签仍然存在,并且标签之间的所有代码都不会被解析。

所以我想知道,我怎样才能正确地做到这一点?


更新: 这是包含的文件的内容:

Testcontent

<?php echo 'This should be parsed, right?'; ?>

【问题讨论】:

  • 您展示的内容应该可以工作 - 代码应该在当前脚本的上下文中执行。你能举例说明包含的文件包含的内容吗?
  • 当你说你想“获取 PHP 文件的内容”时,你到底是什么意思? include()执行一个 PHP 文件,但您是否要像读取文本文件一样读取它?有关您实际尝试实现的目标的更多详细信息将使我们更好地为您提供帮助。您需要帮助来找到问题的解决方案,而不是帮助使预先确定的解决方案无论是否合适都有效。
  • 嗯,正如我所说:我想要解析一个 PHP 文件,并返回内容,所以我可以把它放在一个变量中。
  • 相反,您可以尝试 $var = include 'file.php'; 并在 file.php 中执行 return 'data'; (没有尝试过,但我认为它应该可以工作)。
  • @MikeB $content 包含字符串 &lt;?php echo 'test stuff'; $foo = 'one';。包含文件中的var_dump($foo); 返回NULL

标签: php


【解决方案1】:

几年前,我将这个函数用于某种模板引擎,它似乎可以满足您的需求 - 传递一个包含一些 PHP 代码的字符串,它会在执行 PHP 时返回它。令人惊讶的是,它仍然有效:-)

function process_php( $str )
{
    $php_start = 0;

    $tag = '<?php';
    $endtag = '?>';

    while(is_long($php_start = strpos($str, $tag, $php_start)))
    {
        $start_pos = $php_start + strlen($tag);
        $end_pos = strpos($str, $endtag, $start_pos); //the 3rd param is to start searching from the starting tag - not to mix the ending tag of the 1st block if we want for the 2nd

        if (!$end_pos) { echo "template: php code has no ending tag!", exit; }
        $php_end = $end_pos + strlen($endtag);

        $php_code = substr($str, $start_pos, $end_pos - $start_pos);
        if( strtolower(substr($php_code, 0, 3)) == 'php' )
            $php_code = substr($php_code, 3);

        // before php code
        $part1 = substr($str, 0, $php_start);

        // here set the php output
        ob_start();
        eval($php_code);
        $output = ob_get_contents();
        ob_end_clean();

        // after php code
        $part2 = substr($str, $php_end, strlen($str));

        $str = $part1 . $output . $part2;
    }
    return $str;
}

【讨论】:

  • 这个回复帮助我回答了另一个问题here +1。
  • 如果最后一个语句(或唯一一个语句)没有分号结尾,则引发异常。示例:&lt;?php echo date("Y-m-d") ?&gt;&lt;?php echo "Hello "; echo "World" ?&gt;。如果作为常规 PHP 代码执行,这两个都可以正常工作。认为有人可以解决?我为此做了自己的临时修复,如果它最终不存在,只需添加一个分号,但希望有更好的修复。 :)
【解决方案2】:

根据 Tyil 的最后一条评论,他希望将整个 php 文件包含在一个变量中:

Tyil,如果我的包含文件如下所示:&lt;?php echo 'test stuff'; $foo = 'one';。 $content 包含什么内容以及如果我尝试从包含文件访问 $foo 会发生什么?

@MikeB $content 包含字符串&lt;?php echo 'test stuff'; $foo = 'one';. var_dump($foo); 在包含文件中返回NULL。

<?php
$file = 'include.php';
$content = file_get_contents($file);
var_dump($content); // (string) "<?php echo 'This should be parsed, right?'; $foo = 'one'; ?>"

include.php:

<?php echo 'This should be parsed, right?'; $foo = 'one'; ?>

【讨论】:

  • 这确实是我想要的。我希望最初的问题足以说明这一点。但是在将包含文件的内容设置为&lt;?php echo 'This should be parsed, right?'; $foo = 'one'; ?&gt; 之后,包含它后仍然保留该字符串。我现在不确定这是我的错还是我的网络主机的错:/
【解决方案3】:

如果您可以修改您的$file,则在其中使用return。 否则cURL它(像浏览器一样打开网页)。

【讨论】:

    【解决方案4】:

    这绝对可以。它对我有用:

    [ghoti@pc ~/tmp]$ cat file1.php 
    #!/usr/local/bin/php
    <?php
    
    $file="file2.php";
    include($file);
    
    [ghoti@pc ~/tmp]$ cat file2.php 
    <?php echo "This should be parsed, right?\n"; ?>
    [ghoti@pc ~/tmp]$ ./file1.php 
    This should be parsed, right?
    [ghoti@pc ~/tmp]$ 
    

    您可能想查看包含的文件(以$file 命名),看看在最初的&lt;?php 之后是否可能有一些奇怪的字符可能导致它不被解释为PHP 脚本。

    要查看文件中内容的十六进制转储(这样您就可以看到 &lt;?php 后面的实际字符,而不是编辑器中显示的字符),请使用:od -c filename.php | less

    【讨论】:

      【解决方案5】:

      为此,您必须使用 cURL。

      好吧,无论如何,如果你想有效地做到这一点。

      我知道我迟到了几年,但我也在寻找这个问题的解决方案,我想分享这个解决方案。请记住,虽然此脚本确实解析了 PHP HTML 页面,但通过 Web 协议下载非常慢,至少在我的测试中是这样。

      function GetHTMLPage($url)
      {
          $ch = curl_init();
          $timeout = 5;
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }
      

      ...不要感谢我。感谢大卫沃尔什。 (https://davidwalsh.name/curl-download)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-19
        • 2014-08-21
        • 2016-04-20
        • 1970-01-01
        • 2011-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多