通过PHP从原始文本解析Base64 str

时间:2018-09-01 09:27:50

标签: php base64

如何从以下原始文本中解析或仅获取base64字符串。实际上,我需要 X-Attachment-Id之后的字符串:f_jlj7gz2i2 ,但是 X-Attachment-Id 并不总是相同。所以我不能只是动态地替换或删除它。

> Content-Type: application/pdf; name="Invoice-IN-1.pdf"
> Content-Disposition: attachment; filename="Invoice-IN-1.pdf"
> Content-Transfer-Encoding: base64 Content-ID: <f_jlj7gz2i2>
> X-Attachment-Id: f_jlj7gz2i2
> JVBERi0xLjQKJcLB2s/OCgoxIDAgb2JqIDw8CiAgL1R5cGUgL0NhdGFsb2cKICAvUGFnZXMgMiAw
> IFIKPj4gZW5kb2JqCgoyIDAgb2JqIDw8CiAgL1R5cGUgL1BhZ2VzCiAgL0tpZHMgWyA1IDAgUiBd
> CiAgL0NvdW50IDEKPj4gZW5kb2JqCgozIDAgb2JqIDw8CiAgL1Byb2R1Y2VyIChLZW5kbyBVSSBQ
> REYgR2VuZXJhdG9yKQogIC9UaXRsZSAoKQogIC9BdXRob3IgKCkKICAvU3ViamVjdCAoKQogIC9L
> ZXl3b3JkcyAoKQogIC9DcmVhdG9yIChLZW5kbyBVSSBQREYgR2VuZXJhdG9yKQogIC9DcmVhdGlv
> bkRhdGUgKEQ6MjAxODA4MjgwNzI5NDNaKQo+PiBlbmRvYmoKCjQgMCBvYmogPDwKICAvRmlsdGVy
> IFsgL0ZsYXRlRGVjb2RlIF0KICAvTGVuZ3RoIDE3Mgo+PiBzdHJlYW0KeJx1j8sKAjEMRff5iix1
> MTVpmz62vvZCwQ/QUUbwMcOA+PfWGcURNRdCyeWkN4yUVXBuwbIKETdHqIG7MWPUnpVzhoJEjJGo
> t2soPpyisySK0tLvcdoGH7ApYY2nF2GNMlo4islEGPBighIvxFojuyH1FintHuXx16vZPzPnU75+
> Eq/I5WQ5+zTBZMnIBtMOCFMDo1nV3q7VtsR5eTm3Y0wHWCRY/dEdVZFAMAplbmRzdHJlYW0gZW5k
> b2JqCgo1IDAgb2JqIDw8CiAgL0NvbnRlbnRzIDQgMCBSCiAgL1BhcmVudCAyIDAgUgogIC9NZWRp
> YUJveCBbIDAgMCA1OTUuMjggODQxLjg5IF0KICAvVHlwZSAvUGFnZQogIC9Qcm9jU2V0IFsgL1BE
> RiAvVGV4dCAvSW1hZ2VCIC9JbWFnZUMgL0ltYWdlSSBdCiAgL1Jlc291cmNlcyA8PAogICAgL0Zv
> bnQgPDwKICAgICAgL0YxIDYgMCBSCiAgICA+PgogICAgL0V4dEdTdGF0ZSA8PD4+CiAgICAvWE9i
> amVjdCA8PD4+CiAgICAvUGF0dGVybiA8PD4+CiAgICAvU2hhZGluZyA8PD4+CiAgPj4KICAvQW5u
> b3RzIFsgXQo+PiBlbmRvYmoKCjYgMCBvYmogPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9U
> eXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4gZW5kb2JqCgp4cmVmCjAgNwowMDAwMDAw
> MDAwIDY1NTM1IGYgCjAwMDAwMDAwMTcgMDAwMDAgbiAKMDAwMDAwMDA3MSAwMDAwMCBuIAowMDAw
> MDAwMTM3IDAwMDAwIG4gCjAwMDAwMDAzMjIgMDAwMDAgbiAKMDAwMDAwMDU3NSAwMDAwMCBuIAow
> MDAwMDAwODc0IDAwMDAwIG4gCgp0cmFpbGVyCjw8CiAgL1NpemUgNwogIC9Sb290IDEgMCBSCiAg
> L0luZm8gMyAwIFIKPj4KCnN0YXJ0eHJlZgo5NTMKJSVFT0YK

2 个答案:

答案 0 :(得分:1)

找到X-Attachment字符串键的行索引。然后从该行中提取下一部分。如果您有多个X-Attachment关键字,则必须对此进行一些更改。

这是PHP示例。 (感谢@Andreas警告。)

$data = '
> Content-Type: application/pdf; name="Invoice-IN-1.pdf"
> Content-Disposition: attachment; filename="Invoice-IN-1.pdf"
> Content-Transfer-Encoding: base64 Content-ID: <f_jlj7gz2i2>
> X-Attachment-Id: f_jlj7gz2i2
> JVBERi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GF0ZSA8PD4+CiAgICAvWE9i
> amVjdCA8PD4+CiAgICAvUGF0dGVybiA8PD4+CiAgICAvU2hhZGluZyA8PD4+CiAgPj4KICAvQW5u
> b3RzIFsgXQo+PiBlbmRvYmoKCjYgMCBvYmogPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9U
> eXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4gZW5kb2JqCgp4cmVmCjAgNwowMDAwMDAw
> MDAwIDY1NTM1IGYgCjAwMDAwMDAwMTcgMDAwMDAgbiAKMDAwMDAwMDA3MSAwMDAwMCBuIAowMDAw
> MDAwMTM3IDAwMDAwIG4gCjAwMDAwMDAzMjIgMDAwMDAgbiAKMDAwMDAwMDU3NSAwMDAwMCBuIAow
> MDAwMDAwODc0IDAwMDAwIG4gCgp0cmFpbGVyCjw8CiAgL1NpemUgNwogIC9Sb290IDEgMCBSCiAg
> L0luZm8gMyAwIFIKPj4KCnN0YXJ0eHJlZgo5NTMKJSVFT0YK
';

$lines = explode("\n", $data);
$baseLines = array();
$findedIndex = -1;
$findedBase = "";

for($i = 0; $i < count($lines); $i++) {
    $line = $lines[$i];
    if (strpos($line, "X-Attachment-Id") === false) continue;
    $findedIndex = $i;
}

if ($findedIndex > 0) {

    $baseLines = array_slice($lines, $findedIndex + 1, count($lines));
    $findedBase = implode("\n", $baseLines);
    $findedBase = str_replace(">", "", $findedBase);
    $findedBase = str_replace(" ", "", $findedBase);

    echo $findedBase;

}
else {
    echo 'Couldnt find string for "X-Attachment-Id" in the data.';
}

这里也是C#示例。

    private const string data = 
    @"> Content-Type: application/pdf; name=""Invoice-IN-1.pdf""
    > Content-Disposition: attachment; filename=""Invoice-IN-1.pdf""
    > Content-Transfer-Encoding: base64 Content-ID: <f_jlj7gz2i2>
    > X-Attachment-Id: f_jlj7gz2i2
    > JVBERi0xLjQKJcLB2s/OCgoxIDAgb2JqIDw8CiAgL1R5cGUgL0NhdGFsb2cKICAvUGFnZXMgMiAw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D4+CiAgICAvU2hhZGluZyA8PD4+CiAgPj4KICAvQW5u
    > b3RzIFsgXQo+PiBlbmRvYmoKCjYgMCBvYmogPDwKICAvVHlwZSAvRm9udAogIC9TdWJ0eXBlIC9U
    > eXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4gZW5kb2JqCgp4cmVmCjAgNwowMDAwMDAw
    > MDAwIDY1NTM1IGYgCjAwMDAwMDAwMTcgMDAwMDAgbiAKMDAwMDAwMDA3MSAwMDAwMCBuIAowMDAw
    > MDAwMTM3IDAwMDAwIG4gCjAwMDAwMDAzMjIgMDAwMDAgbiAKMDAwMDAwMDU3NSAwMDAwMCBuIAow
    > MDAwMDAwODc0IDAwMDAwIG4gCgp0cmFpbGVyCjw8CiAgL1NpemUgNwogIC9Sb290IDEgMCBSCiAg
    > L0luZm8gMyAwIFIKPj4KCnN0YXJ0eHJlZgo5NTMKJSVFT0YK";

    static void Main(string[] args)
    {
        var splitLines = data.Split('\n').Cast<String>().ToList();
        var findedBase64 = string.Empty;
        for(int i = 0; i <= splitLines.Count() - 1; i++)
        {
            string line = splitLines[i];
            if (line.IndexOf("X-Attachment-Id") < 0) continue;

            splitLines.RemoveRange(0, i + 1);
            findedBase64 = String.Join("\n", splitLines.ToArray()).Replace(">", "").Replace(" ", "");
            break;
        }
        Console.WriteLine(findedBase64);
        Console.Read();
    }

答案 1 :(得分:0)

<?php
$headers = '> Content-Type: application/pdf; name="Invoice-IN-1.pdf"
> Content-Disposition: attachment; filename="Invoice-IN-1.pdf"
> Content-Transfer-Encoding: base64 Content-ID: <f_jlj7gz2i2>
> X-Attachment-Id: f_jlj7gz2i2
> JVBERi0xLjQKJcLB2s/OCgoxIDAgb2JqIDw8CiAgL1R5cGUgL0NhdGFsb2cKICAvUGFnZXMgMiAw
> IFIKPj4gZW5kb2JqCgoyIDAgb2JqIDw8CiAgL1R5cGUgL1BhZ2VzCiAgL0tpZHMgWyA1IDAgUiBd
> CiAgL0NvdW50IDEKPj4g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U
> eXBlMQogIC9CYXNlRm9udCAvVGltZXMtUm9tYW4KPj4gZW5kb2JqCgp4cmVmCjAgNwowMDAwMDAw
> MDAwIDY1NTM1IGYgCjAwMDAwMDAwMTcgMDAwMDAgbiAKMDAwMDAwMDA3MSAwMDAwMCBuIAowMDAw
> MDAwMTM3IDAwMDAwIG4gCjAwMDAwMDAzMjIgMDAwMDAgbiAKMDAwMDAwMDU3NSAwMDAwMCBuIAow
> MDAwMDAwODc0IDAwMDAwIG4gCgp0cmFpbGVyCjw8CiAgL1NpemUgNwogIC9Sb290IDEgMCBSCiAg
> L0luZm8gMyAwIFIKPj4KCnN0YXJ0eHJlZgo5NTMKJSVFT0YK';

$val = explode("\n", explode('X-Attachment-Id: ', $headers)[1]);
array_shift($val);
$val = implode("\n", $val);

echo $val;

X-Attachment-Id之后接受文本,删除第一行。