I am trying to parse a FDF file using PHP, and regex. But I just cant get my head around regex. I am stuck parsing the file to generate a array.
%FDF-1.2
%âãÏÓ
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V ([email protected])
/T (field_email)
>>
<<
/V (John)
/T (field_name)
>>
<<
/V ()
/T (field_reference)
>>]
>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
Current function (source:http://php.net/manual/en/ref.fdf.php)
function parse2($file) {
if (!preg_match_all("/<<\s*\/V([^>]*)>>/x", $file,$out,PREG_SET_ORDER))
return;
for ($i=0;$i<count($out);$i++) {
$pattern = "<<.*/V\s*(.*)\s*/T\s*(.*)\s*>>";
$thing = $out[$i][1];
if (eregi($pattern,$out[$i][0],$regs)) {
$key = $regs[2];
$val = $regs[1];
$key = preg_replace("/^\s*\(/","",$key);
$key = preg_replace("/\)$/","",$key);
$key = preg_replace("/\\\/","",$key);
$val = preg_replace("/^\s*\(/","",$val);
$val = preg_replace("/\)$/","",$val);
$matches[$key] = $val;
}
}
return $matches;
}
Result:
Array
(
[field_email)
] => [email protected])
[field_name)
] => John)
[field_reference)
] => )
)
Why does it conclude the )
and new line? I know this problem is trivial for someone that understands regex expressions. So help would be appreciated.