PHP 尝试使用 substr 仅显示文本文件中的最后 6 个字符
PHP Trying to use substr to only show last 6 chars in a text file
我是 php 的新手。只是想 运行 这个本地网站。
尝试从每 5 分钟更新一次的 txt 文件中提取最后 5 个字符。
这是我的脚本:
<?php
$temperature = include( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp
?>
这是我的测试文件:
14.75
13.52
这是我的网页显示的内容:
webpage dispaly
关于我做错了什么的任何线索?还是误解了文档?
使用file_get_contents('fileName')
或fopen('fileName', 'mode'); fread('fileName', length);
第一种方法:
将您的 PHP 代码替换为以下内容:
<?php
$temperature = file_get_contents( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp;
?>
您可以在此处找到有关 file_get_contents()
的更多信息:http://php.net/manual/en/function.file-get-contents.php
第二种方法:
<?php
$fileName = "temperaturelog.txt";
$handle = fopen($fileName , 'r');
$temperature = fread($handle, filesize($fileName));
$temp = substr($temperature, -5);
print $temp;
?>
关于第二种方法的更多信息:
http://php.net/manual/en/function.fopen.php
眼前的问题是您使用的是 PHP include。您应该只对 include PHP 可执行代码使用 include - 这就是该函数的用途。它不会 return 文件内容。
实现类似功能的一个非常简单的函数是 file_get_contents, however don't jump the gun just yet. file_get_contents 将读取 整个文件 和 return 它作为一个字符串。非常容易和简单,如果这是您想要做的 - 但您只需要 最后五个字符。因此,我们可以多写一点代码来只读取最后五个字符。这不会太难,我保证!
首先我们要open a file. This will give us a file pointer that we will use to reference that open file in subsequent calls (keep an eye out for it). The following will open a file in read only mode - documentation for fopen.
$fp = fopen('temperaturelog.txt', 'r');
现在,我们有了一个文件指针($fp
),我们可以玩这个文件了。但是我们需要知道 size. There's a number of ways to do this (see fstat, for example) but the simplest way is probably with filesize.
$size = filesize('temperaturelog.txt');
所以现在我们知道 file size. We are going to perform an operation on the file called seek. Just imagine you have your file open in your text editor, we are going to move the cursor to 5 characters before the end - documentation for fseek。
$pos = $size - 5;
fseek($fp, $pos);
现在我们已经设置了 position in the file, we can read until the end of the file. The second parameter of 1024 specifies how many bytes to read from the file. Of course we will only be reading five, however I have set it high to demonstrate how it behaves at the end of a file - see the documentation for fread 以获取有关第二个参数的更多信息。
$text = fread($fp, 1024);
现在我们将最后 5 个字符存储在 $text
中,并且已到达文件末尾。如果确实需要,我们可以 rewind the file pointer, or we could fseek 开始。
完成后关闭文件是个好习惯。这允许其他进程立即打开它,而不是等待 PHP 关闭文件本身 - documentation for fclose.
fclose($fp);
放在一堆片段中,它看起来可能比实际大,所以让我们把所有这些放在一起:
// Open and get size
$fp = fopen('temperaturelog.txt', 'r');
$size = filesize('temperaturelog.txt');
// Seek to just before the end
$pos = $size - 5;
fseek($fp, $pos);
// Read and close
$text = fread($fp, 1024);
fclose($fp);
$size = filesize('temperaturelog.txt');
$file = file_get_contents('temperaturelog.txt');
$text = substr($file, -5);
但是,您可以预计 file_get_contents 示例的可扩展性要低得多。对于简单的东西,它虽然是一种享受!
我是 php 的新手。只是想 运行 这个本地网站。 尝试从每 5 分钟更新一次的 txt 文件中提取最后 5 个字符。 这是我的脚本:
<?php
$temperature = include( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp
?>
这是我的测试文件:
14.75
13.52
这是我的网页显示的内容:
webpage dispaly
关于我做错了什么的任何线索?还是误解了文档?
使用file_get_contents('fileName')
或fopen('fileName', 'mode'); fread('fileName', length);
第一种方法:
将您的 PHP 代码替换为以下内容:
<?php
$temperature = file_get_contents( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp;
?>
您可以在此处找到有关 file_get_contents()
的更多信息:http://php.net/manual/en/function.file-get-contents.php
第二种方法:
<?php
$fileName = "temperaturelog.txt";
$handle = fopen($fileName , 'r');
$temperature = fread($handle, filesize($fileName));
$temp = substr($temperature, -5);
print $temp;
?>
关于第二种方法的更多信息: http://php.net/manual/en/function.fopen.php
眼前的问题是您使用的是 PHP include。您应该只对 include PHP 可执行代码使用 include - 这就是该函数的用途。它不会 return 文件内容。
实现类似功能的一个非常简单的函数是 file_get_contents, however don't jump the gun just yet. file_get_contents 将读取 整个文件 和 return 它作为一个字符串。非常容易和简单,如果这是您想要做的 - 但您只需要 最后五个字符。因此,我们可以多写一点代码来只读取最后五个字符。这不会太难,我保证!
首先我们要open a file. This will give us a file pointer that we will use to reference that open file in subsequent calls (keep an eye out for it). The following will open a file in read only mode - documentation for fopen.
$fp = fopen('temperaturelog.txt', 'r');
现在,我们有了一个文件指针($fp
),我们可以玩这个文件了。但是我们需要知道 size. There's a number of ways to do this (see fstat, for example) but the simplest way is probably with filesize.
$size = filesize('temperaturelog.txt');
所以现在我们知道 file size. We are going to perform an operation on the file called seek. Just imagine you have your file open in your text editor, we are going to move the cursor to 5 characters before the end - documentation for fseek。
$pos = $size - 5;
fseek($fp, $pos);
现在我们已经设置了 position in the file, we can read until the end of the file. The second parameter of 1024 specifies how many bytes to read from the file. Of course we will only be reading five, however I have set it high to demonstrate how it behaves at the end of a file - see the documentation for fread 以获取有关第二个参数的更多信息。
$text = fread($fp, 1024);
现在我们将最后 5 个字符存储在 $text
中,并且已到达文件末尾。如果确实需要,我们可以 rewind the file pointer, or we could fseek 开始。
完成后关闭文件是个好习惯。这允许其他进程立即打开它,而不是等待 PHP 关闭文件本身 - documentation for fclose.
fclose($fp);
放在一堆片段中,它看起来可能比实际大,所以让我们把所有这些放在一起:
// Open and get size
$fp = fopen('temperaturelog.txt', 'r');
$size = filesize('temperaturelog.txt');
// Seek to just before the end
$pos = $size - 5;
fseek($fp, $pos);
// Read and close
$text = fread($fp, 1024);
fclose($fp);
$size = filesize('temperaturelog.txt');
$file = file_get_contents('temperaturelog.txt');
$text = substr($file, -5);
但是,您可以预计 file_get_contents 示例的可扩展性要低得多。对于简单的东西,它虽然是一种享受!