关于使用 PHP 解析 pdftk dump_data_fields
给出的输出的最佳方式,我需要一些建议?
此外,我需要提取的属性是:FieldName
、FieldNameAlt
以及可选的 FieldMaxLength
和 FieldStateOptions
.
FieldType: Text
FieldName: TestName1
FieldNameAlt: TestName1
FieldFlags: 29360128
FieldJustification: Left
FieldMaxLength: 5
---
FieldType: Button
FieldName: TestName3
FieldFlags: 0
FieldJustification: Left
FieldStateOption: Off
FieldStateOption: Yes
---
...
最佳答案
像这样就够了吗?
$handle = fopen("/tmp/bla.txt", "r");
if ($handle) {
$output = array();
while (($line = fgets($handle)) !== false) {
if (trim($line) === "---") {
// Block completed; process it
if (sizeof($output) > 0) {
print_r($output);
}
$output = array();
continue;
}
// Process contents of data block
$parts = explode(":", $line);
if (sizeof($parts) === 2) {
$key = trim($parts[0]);
$value = trim($parts[1]);
if (isset($output[$key])) {
$i = 1;
while(isset($output[$key.$i])) $i++;
$output[$key.$i] = $value;
}
else {
$output[$key] = $value;
}
}
else {
// handle malformed input
}
}
// process final block
if (sizeof($output) > 0) {
print_r($output);
}
fclose($handle);
}
else {
// error while opening the file
}
这将为您提供以下输出:
Array
(
[FieldType] => Text
[FieldName] => TestName1
[FieldNameAlt] => TestName1
[FieldFlags] => 29360128
[FieldJustification] => Left
[FieldMaxLength] => 5
)
Array
(
[FieldType] => Button
[FieldName] => TestName3
[FieldFlags] => 0
[FieldJustification] => Left
[FieldStateOption] => Off
[FieldStateOption1] => Yes
)
找出这些值就像:
echo $output["FieldName"];
关于php - 使用 PHP 解析 pdftk dump_data_fields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34863022/