2

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++;
    }
}
2
  • How big is the file you're working with? Commented Jul 3, 2018 at 10:54
  • Between 700kb and 1mb Commented Jul 3, 2018 at 12:24

3 Answers 3

1

This is simple solution, based on string manipulation:

Input:

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

PHP code:

<?php
// Extract the lines.
$file = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

//
$output = array();
foreach ($file as $row) {
    if (strpos($row, "Serial Number") === false) {
        $n = (count($output)-1);
        if (strpos($row, "Revocation Date") !== false) {
            $date = $row;
            $date = str_replace('Revocation Date: ', ' ', $date);
            $output[$n]['date'] = $date;
        } else if (strpos($row, "CRL entry extensions") !== false) {
        } else if (strpos($row, "X509v3 CRL Reason Code") !== false) {
        } else {
            $output[$n]['reason'] = $row;
        }   
    } else {
        $sn = $row;
        $sn = str_replace('Serial Number: ', ' ', $sn);
        $output[] = array();
        $n = (count($output)-1);
        $output[$n]['serial'] = $sn;
        $n++;
    }   
    echo $row.'</br>';
}

print_r($output);
?>

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 
    ) 
    [2] => Array ( 
        [serial] => 06BB119BAA2ABC21F92B06ED8E14B113 
        [date] => Jun 27 14:49:12 2018 GMT 
        [reason] => Key Compromise 
    ) 
    [3] => Array ( 
        [serial] => 088925C97AC5991CDF5416D07FC5DB00 
        [date] => Jun 27 15:50:51 2018 GMT 
    ) 
    [4] => Array ( 
        [serial] => 091E2B2090C7F5DBBCC97EA958B110BC 
        [date] => Jun 27 15:52:31 2018 GMT
    ) 
    [5] => Array (
        [serial] => 0E6E9D1E9818221538EA6AF16A279C89 
        [date] => Jun 27 15:53:12 2018 GMT 
        [reason] => Key Compromise
    ) 
    [6] => Array ( 
        [serial] => 07852DF7D7DD35080DE3604836408ADE 
        [date] => Jun 27 15:53:38 2018 GMT
    ) 
    [7] => Array (
        [serial] => 0DEA14237257A6A3049F934840DC2B47 
        [date] => Jun 27 15:53:40 2018 GMT 
        [reason] => Key Compromise
    )
)
Sign up to request clarification or add additional context in comments.

Comments

0

Depending on the size of the text file you're using, and how comfortable you are with regex, you could use a pattern that extracts the different bits of information you're looking for.

I've put together a short proof of concept that works for the sample you provided:

$re = '/\W+Serial Number: (?<serial>.*?)$\n\W+Revocation Date: (?<date>.*?)$((?:(?!Serial Number)[\n]*.)+Code: \n\W+(?<reason>.*?$))?/m';

$str = '    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';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

You can see this example in action here: https://regex101.com/r/7iSBrx/1.

This sample uses named groups to facilitate extracting the desired targets from the matches, and also helps illustrate where the target capturing is happening in the pattern. If it's helpful, I'm happy to break down why this pattern works.

As a caveat, this will require loading the entire file into a single string, which may be memory-intensive if the file is large. Your iteration-based approach would be best suited to very large files.

Comments

0

try this code :

 $file_handle = fopen("data.txt", "rb");

    while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $parts = explode('=', $line_of_text);


     $name =array($line_of_text);
    print_r($name);
    }

    fclose($file_handle);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.