Find the specific text until it is found

I was wondering how to do this, this code will, as you know, get a certain line, now I need it to read up to a certain text, for example 55, and stop reading from there. As you can see, the log contains some spaces, and what function can I use to read up to code 55?

$row['MtID'] = unique identifier to indicate the row in which the result is.

So, for example, the result log will be

MM3,67624563 (unique identifier (MtID), 233262345599, http://mywebsite.com:8080/web/mm3_pixel.php?sspdata=ams1CIv44qa26LGkchACGKqShLrCtZieSyINNDEuMTkwLjg4LjIwOCgB&vurlid=993211,http://mywebsite.net/sspx?id=69171&sspdata=ams1CIv44qa26LGkchACGKqShLrCtZieSyINNDEuMTkwLjg4LjIwOCgB > Ok 55

 $logfile = file("https://myweb.com/Pixel-Full.php?file=".$country."/".$today."-pixel-response.log"); foreach($logfile as $line_num = > $line) { if (strpos($line, $row['MtID']) !== false) { $getresult = strstr(htmlspecialchars($line), 'http://'); echo "<td>".$getresult."</td>"; } } 

This system works as follows: the user request didn’t find anything, so in our log it will send a link to the error requested by the user and an error code so that we know what the problem is. Therefore, as soon as the system reads a line and continues to read another line, until it finds the code, it stops

+4
source share
4 answers
  $startline = count($logfile)+1; foreach($logfile as $line_num => $line) { if (strpos($line, $row['MtID']) !== false) { $startline = $line_num; $getresult = trim(strstr(htmlspecialchars($line), 'http://')); if (strpos($getresult, ",55") !== false) { $getresult = substr($getresult,0,strpos($getresult, ",55")+3); break; } } if ($line_num > $startline) { $getresult .= trim(htmlspecialchars($line)); if (strpos($getresult, ",55") !== false) { $getresult = substr($getresult,0,strpos($getresult, ",55")+3); break; } } } echo "<td>".$getresult."</td>"; 
+3
source

You can use the FILE_SKIP_EMPTY_LINES flag in a file call to skip empty lines, and then use array_slice to get the desired part of the array.

 $file = array_slice(file("https://myweb.com/Pixel-Full.php?file={$country}/{$today}-pixel-response.log", FILE_SKIP_EMPTY_LINES), 0, $row['MtID']); foreach($file as $line) { $result = strstr(htmlspecialchars($line), 'http://'); echo "<td>{$result}</td>"; } 
0
source

It seems like stopping execution when a positive match is the biggest trick. This can be done intermittently. ( http://php.net/manual/en/control-structures.break.php )

 //get the file as a string $logfile = file_get_contents("https://myweb.com/Pixel-Full.php?file=".$country."/".$today."-pixel-response.log", false); //make up some rows and catch the false if ($logfile !== false) { $logrows = explode("\n", $logfile); //loop foreach($logrows as $line) { if (strpos($line, $row['MtID']) !== false) { $getresult = strstr(htmlspecialchars($line), 'http://'); echo "<td>".$getresult."</td>"; break; } } } else echo "Log resource unavailable"; //some memory clearing unset($logfile); unset($logrows); 

For common sense, I would suggest that you make sure that the logging format makes the MtID variable something that would not be found in the text of the log if it is not positive. Using a UUID or a specific ID format will work.

0
source

As you thought, sometimes a log line can be divided into three lines, and in some other cases it is only one line:

 // Three lines http://www.yourwebsite.com/error/33/happycodingneverending&errorcode=33, succcess=false; ,55 // one line http://www.yourwebsite.com/error/33/happycodingneverending&errorcode=33,succcess=false;,55 

In this case, you just need to make a small modification to your code, which works fine with an example of only one line to work with these three lines as follows:

 foreach($logfile as $line_num = > $line) { if (strpos($line, $row['MtID']) !== false) { $getresult = strstr(htmlspecialchars($line), 'http://'); if(!$getresult){ $theExactLine = $line_num - 2; $getresult = $logfile[$theExactLine]; } echo "<td>".$getresult."</td>"; break; // to stop looping. } } 
0
source

Source: https://habr.com/ru/post/1499762/


All Articles