使用 php file() 函数将 html 或 php 文件放入数组

Using php file() function to put a html or php file into array

当我尝试使用 php file() 函数时:

在 test.php 中:

$lines = file("./folder/page.php");
foreach ($lines as $line ) {
    echo $line;
}

page.php

<html>
<head>
    <title></title>
</head>
<body>   
<div class="about">
    About: 

</div>
<div class="question">
    Question: 

</div>
</body>
</html>

它returns,

 About:
 Question: 

没有 html 标签,没有 php 行。 目标,拥有一个包含文本编辑器中所有信息的数组 $lines。

您的代码运行良好。 您可能正在浏览器上查看输出,该浏览器假定 HTML 标记是页面的一部分。如果您查看页面源代码,您将看到您获得了所需的输出。 所以转义你的 HTML 个字符你会得到你的输出就好了。

$lines = file("page.php");
foreach ($lines as $line ) {
    echo htmlentities($line)."<br>";
}

为了回答你问我的问题,试试这个:

$arrLines   = array();
$lines      = file("./folder/page.php");

foreach ($lines as $line ) {
    $arrLines[] = $line;
}
var_dump($arrLines);