为什么 PHP 不显示文件的最后一行


Why PHP isn't display the last line of a file?

我想显示XML文件的最后一行,以查看它是否具有有效的结束标记(例如 )

但是我的代码没有播放最后一行。

$xml_input = file('whatever.xml');
$last line = trim(implode("", array_slice($xml_input, -1)));
echo "Last line is : " . $last_line;

该文件存在于 PHP 文件所在的同一位置,file_get_contents可以读取它(因此访问权限没有问题),并且可以使用 echo 显示它。

(尽管奇怪的是,

file_get_contents删除了 xml 标记,并且仅显示标记区域内的信息。

你能帮我我的代码有什么问题吗?

您可以使用 fopen() 和 fread() 来读取 xml 文件,如下所示:

<?php
$myfile = fopen("whatever.xml", "r") or die("Unable to open file!");
// read untill the endand print that 
echo fread($myfile,filesize("whatever.xml"));
// you can save it in a variable and do string handling work.
fclose($myfile);
?>