I have a file with thousands of entries which I am trying to convert to PHP array, however, i have hit a stumbling block as what needs to go in to the array is conditional. The good news is the data is predictable and there are two types of entries 1) Revoked 2) Revoked with Reason
Sample of entry for #1 Revoked
Serial Number: 0E76BE532946EFE890376F0339329A62
Revocation Date: Jun 27 14:46:26 2018 GMT
Sample of entry for #2 Revoked with reason
Serial Number: 0E17C9648FF25C0FC537D97958E4D449
Revocation Date: Jun 27 14:48:07 2018 GMT
CRL entry extensions:
X509v3 CRL Reason Code:
Key Compromise
If revoked with reason it has a total of 5 lines, otherwise only 2 lines.
Sample of data file data.txt
Here is a sample of data from list of thousands of entries which we can use as the sample data file.
Serial Number: 0E76BE532946EFE890376F0339329A62
Revocation Date: Jun 27 14:46:26 2018 GMT
Serial Number: 0E17C9648FF25C0FC537D97958E4D449
Revocation Date: Jun 27 14:48:07 2018 GMT
CRL entry extensions:
X509v3 CRL Reason Code:
Key Compromise
Serial Number: 06BB119BAA2ABC21F92B06ED8E14B113
Revocation Date: Jun 27 14:49:12 2018 GMT
CRL entry extensions:
X509v3 CRL Reason Code:
Key Compromise
Serial Number: 088925C97AC5991CDF5416D07FC5DB00
Revocation Date: Jun 27 15:50:51 2018 GMT
Serial Number: 091E2B2090C7F5DBBCC97EA958B110BC
Revocation Date: Jun 27 15:52:31 2018 GMT
Serial Number: 0E6E9D1E9818221538EA6AF16A279C89
Revocation Date: Jun 27 15:53:12 2018 GMT
CRL entry extensions:
X509v3 CRL Reason Code:
Key Compromise
Serial Number: 07852DF7D7DD35080DE3604836408ADE
Revocation Date: Jun 27 15:53:38 2018 GMT
Serial Number: 0DEA14237257A6A3049F934840DC2B47
Revocation Date: Jun 27 15:53:40 2018 GMT
CRL entry extensions:
X509v3 CRL Reason Code:
Key Compromise
Expected output
I would like to build an array with the following output
Array
(
[0] => Array
(
[serial] => 0E76BE532946EFE890376F0339329A62
[date] => Jun 27 14:46:26 2018 GMT
)
[1] => Array
(
[serial] => 0E17C9648FF25C0FC537D97958E4D449
[date] => Jun 27 14:48:07 2018 GMT
[reason] => Key Compromise
)
...
...
)
Failed attempt
Here is my attempt and only got as far as factoring in the first condition (#1). For (#2) it has the extra lines but couldn't figure out how to factor those in.
$arr = array();
$lines = file('data.txt', FILE_IGNORE_NEW_LINES);
$x = 0;
foreach ($lines as $line) {
if (strpos($line, 'Serial Number: ') !== false) {
$arr[$x]['serial'] = str_replace('Serial Number: ', '', trim($line)) ;
}
if (strpos($line, 'Revocation Date: ') !== false) {
$arr[$x]['date'] = str_replace('Revocation Date: ', '', trim($line)) ;
$x++;
}
}