简体   繁体   中英

PHP trim function not functioning properly

I have the following 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> 

It appears that the if condition on the trim function is not being obeyed. I am debugging by printing the trim function on the line in text file on the page itself. Really don't know what I'm missing.

This is the text file

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

Line 15 should be the exact line of the condition so I do not know what's the problem.

trim is doing exactly what it's supposed to do. The documentation says:

trip whitespace (or other characters) from the beginning and end of a string.

The whitespace differences between your string and the line in the file are in the middle of the string, not the beginning or end.

You can use preg_replace() to compress sequences of whitespace to a single space.

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

Or you could use a regular expression to match the line:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM