I have a text file (a zone file actually) which looks like this:
...
;
; base config
; -----------------------------------------------------------------
@ 14400 IN A 1.2.3.4
@ 14400 IN AAAA 1:2:3::4
;
; mail config
; -----------------------------------------------------------------
mail 14400 IN A 1.2.3.4
mail 14400 IN AAAA 1:2:3::4
@ 14400 IN MX 10 mail.example.com.
;
; www config
; -----------------------------------------------------------------
www 14400 IN CNAME example.com.
...
I would like to parse each not commented out lines into an array, block by block. So it should look like this:
$array = array(
"base" => array(
"0" => "@ 14400 IN A 1.2.3.4",
"1" => "@ 14400 IN AAAA 1:2:3::4"
),
"mail" => array (
"0" => "mail 14400 IN A 1.2.3.4",
"1" => "mail 14400 IN AAAA 1:2:3::4",
"1" => "@ 14400 IN MX 10 mail.example.com."
),
"www" => array(
"0" => "www 14400 IN CNAME example.com."
)
);
In this case, the comments are ";" so it does not need to parse at all. The commented out sections are fix, like the "base config" and "mail config" and the "www config" does not changing, it looks every time as in my example. But the records (not ommented out with ;) in each block can be changing, so it is possible to have only 1 record, or 5, 10, or any. I did a try to put the text file into an array an process it with a foreach loop, then search the corresponding lines with preg_match() but it doesn not solved, because I don't know how many records (lines) comes until the next section of the file. Her is my try:
<?php
// Get lines
$lines = file('file.txt');
// Loop through our array
foreach ($lines as $line_num => $line) {
if (preg_match("/\bbase config\b/i", $line)) {
$line++
// I don't know how to continue and find the next section
}
}
?>
If you could help me to process one block (base, mail or www) it will help me much, based on that, I can solve the others.