我在文本文件中有以下字符串:

时间:2014-03-07 20:04:13

标签: php

我有一个代码,但没有工作,我正在尝试使用爆炸但仍然无法正常工作。 我的代码有两个字符串:

^@^b = (on)
^A^b = (off) 

我尝试在变量中只从.txt文件中获取2个字符,如:

$on=^@
$off=^A

然后我可以使用结果来验证使用调光器的光的状态。

代码

<?php
if(isset($_POST['estado'])) {
exec('/var/www/www.rfteste.com/htdocs/estado.sh');
}
if(isset($_POST['ligar'])) {
exec('/var/www/www.rfteste.com/htdocs/liga.sh');
}
if(isset($_POST['desligar'])) {
exec('/var/www/www.rfteste.com/htdocs/desliga.sh');
}
echo "<H3>CONTROL PANEL</H3>";
$str = file_get_contents("/var/www/www.rfteste.com/htdocs/estado.txt");
$vals = explode("^", $str); 
$num1 = "^".$vals[0];
$num2 = "^".$vals[1];
$onoff= "^A";
if($num2 == $onoff)
echo "<b>on</b>";
else
echo "<b>off</b>";
?>
<html>
<body>
<form method="post" action="">
<p>
<center><input type="submit" value="Ligar" name="ligar""';" /></center>
<center><input type="submit" value="desligar" name="desligar""';" /></center>
<center><input type="submit" value="atualizar" name="estado""';" /></center>

2 个答案:

答案 0 :(得分:0)

我认为这可以帮助您解决问题

function checktext($text){
    if(preg_match("'@'", $text)){
        return 'on';
    }else{
        return 'off';
    }
}
echo checktext('^A^b');

答案 1 :(得分:0)

使用php substr()

if(substr($str, 0, 1) == "A")
{
echo "<b>on</b>";
} else {
echo "<b>off</b>";
}