PHP trim函数运行不正常


PHP trim function not functioning properly

我有以下php/html页面

<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行应该是条件的确切行,所以我不知道问题出在哪里。

trim正在做它应该做的事情

从字符串的开头和结尾去掉空白(或其他字符)。

字符串和文件中的行之间的空格差异在字符串的中间,而不是开头或结尾。

可以使用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]) {