0

I have a file $ocsp_resp_file with the following content:

1432599306989.cer.tmp: good
    This Update: May 23 12:54:14 2015 GMT
    Next Update: May 30 12:54:14 2015 GMT

I am only interested in the lines starting with "This Update:" and "Next Update:". I would like to retrieve those lines and strip out the spaces and text before the timestamp.

It should end up looking like this:

May 23 12:54:14 2015 GMT
May 30 12:54:14 2015 GMT

I am using the following code to return each one but not quite how i want it.

$search = 'This Update';
        $lines = file($ocsp_resp_file);
        foreach($lines as $line)
        {
        $line = ltrim($line);
          if(strpos($line, $search) !== false)
          //$line = str_replace('This Update: ', '', $line);
            echo $line;
        }

The above code returns this:

This Update: May 23 12:54:14 2015 GMT

I then tried to remove "This Update: " using the following code (now commented out) but it breaks the logic of the code and each line is returned

$line = str_replace('This Update: ', '', $line);

user3436467
  • 1,763
  • 1
  • 22
  • 35
  • this is marked as duplicate?? the references solution is for a C# language and couldn't see anywhere how it answers my question?? – user3436467 May 26 '15 at 00:31
  • agreed. looks like @Barmar made a mistake here -ill add an answer link shortly –  May 26 '15 at 00:34
  • 1
    The reason for the problem is because you didn't wrap the `if` body in braces. So when you added the `$line = str_replace` line, the `echo` was no longer in the `if`, so every line was echoed. That's why this is a duplicate of that other question. – Barmar May 26 '15 at 00:39
  • That's why it's a bad practice to leave out the braces: when you add another line, and forget to add the braces, you get this problem. – Barmar May 26 '15 at 00:41
  • Thanks Barmer. Adding the `{ }` resolve the issue. – user3436467 May 26 '15 at 00:44
  • 1
    alternate approach: http://ideone.com/XFxA6v –  May 26 '15 at 01:16
  • nice. thanks for the alternative way. – user3436467 May 26 '15 at 01:22

0 Answers0