PHP修剪功能无法正常运行

时间:2016-05-05 23:34:03

标签: php if-statement trim

我有以下php / html page

<html>
 <head>
  <title>DZ Prototype</title>
<link rel="icon" type="img/ico" href="images/favicon.jpg">
<meta http-equiv="refresh" content="30">
 </head>
 <body>
<center>
<h1>Welcome to DZ Prototype Testing Area!!</h1>
</center>
<p></p>
<p></p>
<p></p>
<p style="text-align:center"><img src="https://diasp.eu/uploads/images/scaled_full_122e075ce77580c93020.jpeg" alt="It works!!"></p>
<p></p>
<p></p>

<div align="center">
<?php

$filename = "cowrie.txt";
$line = file($filename);
echo $line[15];

if(file_exists($filename)){
echo "<h2>Read through file and</h2>";
}
else{
echo "<h2>Upload not successful</h2>";
}

echo trim($line[15]);

if (trim($line[15]) == "Correctly Classified Instances 0 0 %") { 
    echo "<h2><font color='red'>Last entry is a Possible Malicious Login Attempt</h2>";
} else {
    echo "<h2><font color='green'>Status Green</h2>";
}

?>
</div>

</body>
</html> 

看来修剪功能上的if条件没有被遵守。我正在通过在页面本身的文本文件中的行上打印trim函数进行调试。真的不知道我错过了什么。

这是文本文件

J48 pruned tree
------------------

AttemptsOnIP <= 6: 0 (9.0)
AttemptsOnIP > 6: 1 (20.0)

Number of Leaves  :     2

Size of the tree :  3



=== Error on test data ===

Correctly Classified Instances           0                0      %
Incorrectly Classified Instances         1              100      %
Kappa statistic                          0     
Mean absolute error                      1     
Root mean squared error                  1     
Total Number of Instances                1     


=== Detailed Accuracy By Class ===

                 TP Rate  FP Rate  Precision  Recall   F-Measure  MCC      ROC Area  PRC Area  Class
                 0.000    0.000    0.000      0.000    0.000      0.000    ?         1.000     0
                 0.000    1.000    0.000      0.000    0.000      0.000    ?         ?         1
Weighted Avg.    0.000    0.000    0.000      0.000    0.000      0.000    0.000     1.000     


=== Confusion Matrix ===

 a b   <-- classified as
 0 1 | a = 0
 0 0 | b = 1

第15行应该是条件的确切行,所以我不知道问题是什么。

1 个答案:

答案 0 :(得分:2)

trim正在做他应该做的事情。 documentation说:

  

从字符串的开头和结尾开始行空格(或其他字符)。

字符串与文件中的行之间的空白差异位于字符串的中间,而不是开头或结尾。

您可以使用preg_replace()将空格序列压缩到单个空格。

if (preg_replace('/\s+/, ' ', $line[15]) == "Correctly Classified Instances 0 0 %") { 

或者您可以使用正则表达式来匹配该行:

if(preg_match('/Correctly Classified Instances\s+0\s+0\s+%/', $line[15]) {
相关问题