首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP尝试使用substr仅显示文本文件中的最后6个字符

PHP尝试使用substr仅显示文本文件中的最后6个字符
EN

Stack Overflow用户
提问于 2018-07-24 07:33:22
回答 1查看 183关注 0票数 -3

我是php的新手。只是想运行这个本地网站。尝试从每5分钟更新一次的txt文件中提取最后5个字符。这是我的脚本:

    <?php 
$temperature = include( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp
?>

这是我的测试文件:

14.75
13.52

以下是我的网页显示的内容:

webpage dispaly

关于我做错了什么有什么线索吗?或者误解了文档?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-24 07:46:59

使用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

http://php.net/manual/en/function.fread.php

http://php.net/manual/en/function.filesize.php

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51488491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档