Use base64_decode decoding of PHP to return false

[problems encountered]

try to use PHP"s base64_decode function to decode the content of a message that is encoded with Base64, but find that some messages return false. According to my test, it seems that the parts containing non-Base64 codes can be decoded normally after they are deleted, but according to the documentation--

string base64_decode ( string $data [, bool $strict = false ] )

when strict is set to TRUE, FALSE is returned once the data entered exceeds the base64 alphabet. Otherwise, will invalid characters be silently discarded?

what should I do if I want to decode messages with non-encoded content?

[related codes]

example of the content of the message to be decoded. only those "- = _ Part_." that are deleted from the beginning and tail. To decode successfully, otherwise false is returned.

<?php
  $str="------=_Part_52_1608986047.1312859369078 Content-Type: multipart/alternative; boundary="----=_Part_53_1114408905.1312859369078" ------=_Part_53_1114408905.1312859369078 Content-Type: text/plain; charset="gb18030" Content-Transfer-Encoding: base64 1eLKx9K7t+JIVE1MuPHKvdPKvP6jrMfrx9C7u7W9SFRNTMrTzbw ------=_Part_53_1114408905.1312859369078 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: base64 PG1ldGEgY2hhcnNldD0iVVRGLTgiIC8+PHRhYmxlIGFsaWduPSJjZW50ZXIiIHN0eWxlPSJmb250 LWZhbWlseTpNaWNyb3NvZnQgWWFIZWksU2ltc3VuO3dpZHRoOjc1MHB4O3RhYmxlLWxheW91dDpm aXhlZDsiICBiZ2NvbG9yPSIjZmZmZmZmIiBjZWxscGFkZGluZz0iMCIgY2VsbHNwYWNpbmc9IjAi Pjx0Ym9keT48dHI+PHRkIHN0eWxlPSJkaXNwbGF5Om5vbmU7Ij7lhaXpl6gyOTwvdGQ+PC90cj4g PHRyPjx0ZD48dGFibGUgc3R5bGU9IndpZHRoOjc1MHB4OyIgYm9yZGVyPSIwIiBjZWxscGFkZGlu Zz0iMCIgY2VsbHNwYWNpbmc9IjAiPjx0cj48dGQgc3R5bGU9IndpZHRoOjc1MHB4O2hlaWdodDox MDBweDtmb250LWZhbWlseTpNaWNyb3NvZnQgWWFIZWksU2ltc3VuO2JhY2tncm91bmQ6IzE3MjAz ZjsiPjxpbWcg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plho3mlLbliLDor6Xkuqflk4HnmoTmjqjo jZDpgq7ku7bvvIzor7fngrnlh7sgPGEgc3R5bGU9ImZvbnQtc2l6ZToxMHB4IiBocmVmPSJodHRw Oi8vZG0ubWFpbC4xNjMuY29tL2Fub255bW91cy9hZHN1YnNjcmliZS91bnN1YnNjcmliZT9kZXBJ ZD0xJnByb0lkPTEwNjAwMyZtaXRJZD02MDU2NTM4OCZzZWM9QjJkTkFCeW10WWg3UzlZa0Y2QWpl R1RqNzRqZWRTMGwiIGhpZGVmb2N1cz0idHJ1ZSI+6L+Z6YeM6YCA6K6iPC9hPjwvZGl2Pg0KPC9k aXY+DQo------=_Part_53_1114408905.1312859369078-- ------=_Part_52_1608986047.1312859369078--
";

  $text=base64_decode($str);
  var_dump($text);
  echo $text;
?>

[attempts made]

switching to imap_base64 was also unsuccessful.

[Test Environment]

XAMPP
operating system: Windows 10 Family Chinese version 1803
Apache version: 2.4.16 (Win32)
PHP version: 5.6.12

Jan.15,2022

is not how to decode base64.


your $str variable obviously needs to be parsed and then decoded.
if I guess correctly, the original text should be cut according to the newline character, where Content-Transfer-Encoding identifies the encoding format, and the body is the string to be decoded.
it is recommended that you separate the parts before decoding them.


the question that base64_decode decoding returns false has been answered clearly by Shangguanyuan Heng .

with regard to the problem of decoding email, I solved it myself by studying the user comments and continuous testing under imap_fetchstructure and imap_fetchbody in the PHP manual.

generally speaking, email is divided into simple and multipart. The encoding obtained by simple using imap_fetchstructure is the actual encoding used by the email; while the email from multipart may contain multiple parts, and each part may contain multiple parts,. The type and encoding of each part of the part may be different, so it needs to be parsed one by one through a loop. Each part can be decoded according to the corresponding encoding, so that there is no problem of non-coding content in the content to be decoded!

can be combined with reference--

  1. comments on user " david at hundsness dot com " under the imap_fetchstructure description in the
  2. PHP manual: PHP:imap_fetchstructure-Manual
  3. comments on user " atamido at gmail dot remove dot com " under the imap_fetchbody description in the
  4. PHP manual: PHP:imap_fetchbody-Manual
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3918b-2c175.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3918b-2c175.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?