PHP Search for string in file and print lines above/below match line

38
March 16, 2019, at 02:00 AM

Question:

I am trying to search for a string in a file and print only the first line above and second line below the match line for all occurrences of the string. Is there a way I can achieve this with a PHP script? (will be updating a web page with output) couldn't find a solution other than via shell grep/awk which I can't use.

File content:

"timeUpdated":1533998971008
"name":"ABC"
"uptime":5143980267
"desiredStatus":"STARTED"
"lastReportedStatus":"STARTED"
"started":true
"serverArtifacts":[{"id":73
"timeUpdated":1525213888405
"name":"XYZ"
"uptime":6746828144F
"desiredStatus":"STARTED"
"lastReportedStatus":"STARTED"
"started":true 
"serverArtifacts":[{"id":21 
"timeUpdated":1544131291380
 "name":"KLM"
"uptime":6746828643
"desiredStatus":"STARTED"
"lastReportedStatus":"STARTED"
"started":true "serverArtifacts":[{"id":303
search for string: uptime -> print first line above (name:) and second line blow (lastReportedStatus:)
note: I can only use "uptime" as a search string since all other value pairs occur multiple times in a different sequence on other parts of the file.
expected output:
"name":"ABC"
"lastReportedStatus":"STARTED"
"name":"XYZ"
"lastReportedStatus":"STARTED"
"name":"KLM"
"lastReportedStatus":"STARTED"
Answer 1

If you read the lines in the file into an array:

$lines = file('file.txt', FILE_IGNORE_NEW_LINES);

Then you can just keep track of the key as you iterate the array, and find the lines relative to the line with "uptime" using simple addition/subtraction on the key.

foreach ($lines as $key => $line) {
    if (substr(trim($line),0, 8) == '"uptime"') {
        $result[] = $lines[$key - 1];
        $result[] = $lines[$key + 2] ?? '';
    }
}

You may need to add the FILE_SKIP_EMPTY_LINES option to file() if there really are blank lines between each line your file. I wasn't sure if it was just like that in the question here.

I'm curious where the file data comes from, though. It's very close to JSON, and if there was any way to get it the rest of the way there at the source, we wouldn't have to deal with it in such a hacky way here. This code will work for the example you showed, but it's not ideal.

READ ALSO
Redirect automatically from Woocommerce thankyou to an external link passing variables

Redirect automatically from Woocommerce thankyou to an external link passing variables

In Woocommerce, after placing an order, I Would like to redirect customer automatically after 5 sec from the thankyou page to external link passing a few variables as the order_id, and the order_ammount

12
what is the best database management systeme to use for large chat website? [on hold]

what is the best database management systeme to use for large chat website? [on hold]

actually i ma sedigning a new database for a quite large chat website with some friends, but we think that phpmyadmin isn't that good for, so i need to what DBMS is the best for that, for example what facebook database uses?

17
PHP - Iterate through file and break out chunks of text for JSON

PHP - Iterate through file and break out chunks of text for JSON

I have a file on a server that I need to parse and build a JSON object to returnI am using PHP

47
PHP - How to Change Results from While Loop if Any Row Result Matches

PHP - How to Change Results from While Loop if Any Row Result Matches

I've looked for the result for this, and can't seem to find anything dealing with this specific issue

24