在 PHP 中使用 HTML 标签
Using HTML tags inside PHP
背景
我有一个 playlog.csv 文件位于多个 Raspberry PI 上,格式如下:
2018-03-22 12:43:21,NM_Test.h264,-2 //PI 1
2018-03-22 12:43:21,NM_Test.h264,-2 //PI 2
2018-03-22 12:43:21,vid.h264,0 //PI 3
我可以通过以下方式连接到每个 PI 并跟踪 CSV 文件:
<DOCTYPE html>
<html>
<style>
#circleRed {
background: #ff0000;
width: 15px;
height: 15px;
border-radius: 50%;
}
#circleGreen {
background: #00ff00;
width: 15px;
height: 15px;
border-radius: 50px;
}
</style>
<?php
require_once 'Net/SSH2.php';
require_once 'phpseclib1.0.10/Crypt/RSA.php';
$config = require 'config.php';
$log = 'logfile.txt';
if(is_array($config)){
foreach($config as $cred){
$ssh = new Net_SSH2($cred['ip'], $cred['port']); //get the IP and port
$key = new Crypt_RSA();
$key->loadKey($cred['key']);
if (!$ssh->login('pi', $key)){
//logging with file_put_contants, Append mode, exclusive lock is more race condition safe then an open file handle.
file_put_contents($log, "[".date('Y-m-d H:i:s')."]Login Failed for {$cred['ip']}\n", FILE_APPEND|LOCK_EX);
continue;
}
$output = $ssh->exec('tail -1 /var/log/playlog.csv');
}
};
$array = explode(',',$output);
if(in_array('0', $array, true)){
echo '<div id="circleGreen"></div>';
}
if (in_array('-2'||'-3'||'-4'||'-5', $array, true)){
echo '<div id="circleRed"></div>';
}
?>
</body>
</html>
问题
查看最右边的值,如果值为“-2”或“-3”等,我希望显示一个红色圆圈,但如果值为“0”,我会喜欢在我的网页上显示一个绿色圆圈。我正在尝试对通过 SSH 连接的所有 PI 执行此操作。
但目前当我 运行 我的代码时,我得到一个空白网页,我不知道我做错了什么?
你需要小心 in_array()
的严格模式,因为它是类型敏感的。对于您的情况,您可以只检查最后一个元素是否小于零。这是一个例子。尽管在 foreach 循环中执行所有操作以检查每个 pi 的 return 值。
foreach (...) {
...
$output = $ssh -> exec ('tail -1 /var/log/playlog.csv');
$array = explode (',', $output);
if (end ($array) >= 0) {
echo '<div id="circleGreen"></div>';
} else {
echo '<div id="circleRed"></div>';
}
}
行
in_array('-2'||'-3'||'-4'||'-5', $array, true)
并没有按照您的想法行事。 in_array
只能为 $needle
参数接受一个值 - 此行会将初始表达式计算为布尔值 true,然后检查 $array
是否包含该确切值。
如果你想检查两个数组之间是否有任何重叠(即值 -2、-3、-4 或 -5 是否存在于分解线内的任何位置),你可以使用 array_intersect
,例如
if (count(array_intersect(['-2', '-3', '-4', '-5'], $array))) {
...
背景
我有一个 playlog.csv 文件位于多个 Raspberry PI 上,格式如下:
2018-03-22 12:43:21,NM_Test.h264,-2 //PI 1
2018-03-22 12:43:21,NM_Test.h264,-2 //PI 2
2018-03-22 12:43:21,vid.h264,0 //PI 3
我可以通过以下方式连接到每个 PI 并跟踪 CSV 文件:
<DOCTYPE html>
<html>
<style>
#circleRed {
background: #ff0000;
width: 15px;
height: 15px;
border-radius: 50%;
}
#circleGreen {
background: #00ff00;
width: 15px;
height: 15px;
border-radius: 50px;
}
</style>
<?php
require_once 'Net/SSH2.php';
require_once 'phpseclib1.0.10/Crypt/RSA.php';
$config = require 'config.php';
$log = 'logfile.txt';
if(is_array($config)){
foreach($config as $cred){
$ssh = new Net_SSH2($cred['ip'], $cred['port']); //get the IP and port
$key = new Crypt_RSA();
$key->loadKey($cred['key']);
if (!$ssh->login('pi', $key)){
//logging with file_put_contants, Append mode, exclusive lock is more race condition safe then an open file handle.
file_put_contents($log, "[".date('Y-m-d H:i:s')."]Login Failed for {$cred['ip']}\n", FILE_APPEND|LOCK_EX);
continue;
}
$output = $ssh->exec('tail -1 /var/log/playlog.csv');
}
};
$array = explode(',',$output);
if(in_array('0', $array, true)){
echo '<div id="circleGreen"></div>';
}
if (in_array('-2'||'-3'||'-4'||'-5', $array, true)){
echo '<div id="circleRed"></div>';
}
?>
</body>
</html>
问题
查看最右边的值,如果值为“-2”或“-3”等,我希望显示一个红色圆圈,但如果值为“0”,我会喜欢在我的网页上显示一个绿色圆圈。我正在尝试对通过 SSH 连接的所有 PI 执行此操作。
但目前当我 运行 我的代码时,我得到一个空白网页,我不知道我做错了什么?
你需要小心 in_array()
的严格模式,因为它是类型敏感的。对于您的情况,您可以只检查最后一个元素是否小于零。这是一个例子。尽管在 foreach 循环中执行所有操作以检查每个 pi 的 return 值。
foreach (...) {
...
$output = $ssh -> exec ('tail -1 /var/log/playlog.csv');
$array = explode (',', $output);
if (end ($array) >= 0) {
echo '<div id="circleGreen"></div>';
} else {
echo '<div id="circleRed"></div>';
}
}
行
in_array('-2'||'-3'||'-4'||'-5', $array, true)
并没有按照您的想法行事。 in_array
只能为 $needle
参数接受一个值 - 此行会将初始表达式计算为布尔值 true,然后检查 $array
是否包含该确切值。
如果你想检查两个数组之间是否有任何重叠(即值 -2、-3、-4 或 -5 是否存在于分解线内的任何位置),你可以使用 array_intersect
,例如
if (count(array_intersect(['-2', '-3', '-4', '-5'], $array))) {
...