php - 使用 PHP 解析 pdftk dump_data_fields?

标签 php parsing text-parsing pdftk

关于使用 PHP 解析 pdftk dump_data_fields 给出的输出的最佳方式,我需要一些建议?

此外,我需要提取的属性是:FieldNameFieldNameAlt 以及可选的 FieldMaxLengthFieldStateOptions.

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/

相关文章:

php - 获取指定节点类型的最新 10 篇文章?

php - 如何让 Amazon SQS PHP 接收器脚本永远运行?

xml - android sdk main.out.xml 解析错误?

Python文本处理/查找数据

python - 使用 python 将 bibtex 文件转换为 html(也许是 pybtex?)

php - 错误导致页面关闭并重新打开 - PHP/HTML

phpMyAdmin - 错误 |缺少 mbstring 扩展名。请检查您在 Ubuntu 14.04 LTS 中的 PHP 配置

c# - CSV 文件的强类型解析

javascript - 使用javascript在html中获取子节点和子节点

r - 从文本中提取 "((Adj|Noun)+|((Adj|Noun)(Noun-Prep)?)(Adj|Noun))Noun"(Justeson & Katz, 1995)