简体   繁体   English

正则表达式从PHP imap的电子邮件正文中提取base64编码的部分

[英]Regex to extract base64 encoded parts from email body from php imap

I have the following sting below, It's an e-mail body and I want to extract only the base64 encoded files from it in PHP. 我在下面有以下提示,这是一个电子邮件正文,我想从PHP中仅提取base64编码的文件。 I was thinking I could use regex to just extract the base64 parts so I could then decode them eg 我以为我可以使用正则表达式来提取base64部分,以便随后对它们进行解码,例如

preg_match('/Content-Transfer-Encoding: base64\s(.*)\sContent-Type:/', $message['body'], $matches);

echo base64_decode($matches[0]);

However I'm not getting any matches as I don't think my regex is working. 但是我没有找到任何匹配项,因为我认为我的正则表达式不起作用。 Ideally I want to capture each part which starts after 'Content-Transfer-Encoding:' and ends with another 'Content-Type:' part starting. 理想情况下,我想捕获从“ Content-Transfer-Encoding:”之后开始并以另一个“ Content-Type:”部分开始的每个部分。 My question is, how do I regex all the base64 parts? 我的问题是,我该如何对所有base64部分进行正则表达式? eg 例如

Content-Transfer-Encoding : (capture this part as a match) Content-Type: Content-Transfer-Encoding :(将此部分捕获为匹配项)Content-Type:

Below is part of the message and the full string can be found here: 以下是消息的一部分,可以在此处找到完整的字符串:

http://pastebin.com/A5XXnSaT http://pastebin.com/A5XXnSaT

--_004_D16F6E4A2986D34F9D752E3564EAC46F359618EFAPP1197ghakfplc_ Content-Type: multipart/alternative; --_ 004_D16F6E4A2986D34F9D752E3564EAC46F359618EFAPP1197ghakfplc_内容类型:多部分/替代; boundary="_000_D16F6E4A2986D34F9D752E3564EAC46F359618EFAPP1197ghakfplc_" --_000_D16F6E4A2986D34F9D752E3564EAC46F359618EFAPP1197ghakfplc_ Content-Type: text/plain; boundary =“ _ 000_D16F6E4A2986D34F9D752E3564EAC46F359618EFAPP1197ghakfplc_” --_ 000_D16F6E4A2986D34F9D752E3564EAC46F359618EFAPP1197ghakfplc_内容类型:文本/纯文本; charset="utf-8" Content-Transfer-Encoding: base64 SGksIFBsZWFzZSBmaW5kIHRoZSBhdHRhY2hlZCBQRlMgZXJyb3IgUmVwb3J0DQoKLS0tLS0tLS0t LQpCJlEgcGxjClJlZ2lzdGVyZWQgT2ZmaWNlOiBCJlEgSG91c2UsIENoZXN0bnV0IEF2ZW51ZSwg Q2hhbmRsZXJzIEZvcmQsIEVhc3RsZWlnaCwgSGFtcHNoaXJlLCBTTzUzIDNMRQpCdXkgT25saW5l IE5PVyBhdCB3d3cuZGl5LmNvbSBvciBmb2xsb3cgQiZRIG9uIEZhY2Vib29rIHd3dy5mYWNlYm9v... 字符集= “utf-8” 内容传输编码:的base64 SGksIFBsZWF​​zZSBmaW5kIHRoZSBhdHRhY2hlZCBQRlMgZXJyb3IgUmVwb3J0DQoKLS0tLS0tLS0t LQpCJlEgcGxjClJlZ2lzdGVyZWQgT2ZmaWNlOiBCJlEgSG91c2UsIENoZXN0bnV0IEF2ZW51ZSwg Q2hhbmRsZXJzIEZvcmQsIEVhc3RsZWlnaCwgSGFtcHNoaXJlLCBTTzUzIDNMRQpCdXkgT25saW5l IE5PVyBhdCB3d3cuZGl5LmNvbSBvciBmb2xsb3cgQiZRIG9uIEZhY2Vib29rIHd3dy5mYWNlYm9v ...

Outputs 6 decoded parts 输出6个解码部分

$result=[];
preg_match_all('/Content-Transfer-Encoding: base64\s(.*?)\s--/', $message['body'], $matches);

foreach ($matches[1] as $base) {
    $result[] = base64_decode($base);
}

print_r($result);

Example: https://eval.in/438478 示例: https//eval.in/438478

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM