我目前遇到了一个问题,我在文件中有一些php代码并从另一个文件中调用该文件。
这样:
index.php正在调用file.php,而file.php中有一些php代码。
我使用file_get_contents的原因是我在index.php中有代码,只需要读取file.php中的一些内容,这些内容由标签指定。基于由($ _GET ['identifier'])触发的teh section标签; file.php中的那个部分是唯一显示的部分
file.php中的示例代码:
<?php
//simplified php code
var $item = "hello";
var $item2 = "Hola";
var $item3 = "おはよ";
?>
<section id="content">
<?php echo $item1; ?>
</section>
<section id="content2">
<?php echo $item2; ?>
</section>
<section id="content3">
<?php echo $item3; ?>
</section>
?>
index.php中的代码:
$content = file_get_contents('file.php');
if (isset($_GET['item'])) {
$section = $_GET['item'];
} else {
$section = 'content';
}
$delimiter = '<section id="' . $section . '">';
$start = explode($delimiter, $content);
$end = explode("</section>", $start[ 1 ] );
echo $end[0];
因此,如果浏览器网址显示index.php?item = content2,则应显示名为content2的部分ID中的内容以及该文件中的PHP代码。
目前,如果我这样做,则不显示任何内容,并且视图源显示php代码
答案 0 :(得分:3)
要在获取内容之前执行代码,您需要include
并捕获输出:
ob_start();
include('file.php');
$content = ob_get_clean();
或者通过URL获取文件(可能不是最好的主意,也不是可移植的):
$content = file_get_contents('http://example.com/file.php');