Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅更改来自txt的字符串的最后一个字符_Php_Html_String_Replace - Fatal编程技术网

Php 仅更改来自txt的字符串的最后一个字符

Php 仅更改来自txt的字符串的最后一个字符,php,html,string,replace,Php,Html,String,Replace,我有以下返回txt文件内容的代码: $myfile = fopen("api/files/".$_GET["name"]."/log.txt", "r") or die("Error"); 回报是这样的 2021/03/25 00:31 Status : 0 2021/03/25 00:45 Status : 1 2021/03/25 00:45 Status :

我有以下返回txt文件内容的代码:

    $myfile = fopen("api/files/".$_GET["name"]."/log.txt", "r") or die("Error");
回报是这样的

    2021/03/25 00:31 Status : 0
    2021/03/25 00:45 Status : 1
    2021/03/25 00:45 Status : 0
    2021/03/25 00:45 Status : 1
我需要将最后一个字符(1或0)更改为打开或关闭

并取决于。$\u获取[“名称”]。(火灾传感器、运动传感器…)我需要将其更改为检测到/未检测到(对于运动传感器)

我试过使用:

echo substr_replace('0', "Open", -1);

但是不工作

第一个参数应该是要编辑的初始字符串。在这里,您传递
'0'
字符串,该字符串没有任何匹配的字符串可替换

$txt = "2021/03/25 00:31 Status : 0";
echo substr_replace($txt, "Open", -1);
输出:
2021/03/25 00:31状态:打开


文档参考:

因为你通过
0
作为你的源代码,你应该通过
$myFile
content第一个调用端口应该始终是我们通常不回答打字错误的问题,我们只做一个简单的注释来纠正操作,然后关闭并删除问题,因为打字错误对其他人几乎没有任何实际用途,这是SOI的要点,不幸的是需要50名代表发表评论。