Shawn Shawn - 1 year ago 55
PHP Question

PHP: how to pull base64 encoded messages?

I have some basic raw base64 encoded emails that I want to decode and convert to PDF format. The problem I am running into, is the

$str
variable. That line of code is the first 76 characters in all my emails, but I can't figure out to use that as a starting base to scan / gather / collect the rest of the encryption code.

So basically $str is the starting point, but then there is 1000's of more lines of base64 encryption coding, and I want to say, if you FIND THIS, then start pulling all the REST of the code. I looked into strpos and substr, but they use direct numerical values. Is it possible to use characters as starting positions?

To sum up: I basically want to say if you find $str in the document, copy the the rest of the code, starting from where you found it.

$str = "JVBERi0xLjcKJeTjz9IKNCAwIG9iago8PC9MZW5ndGggMyAwIFIvRmlsdGVy";

$pdf_base64 = "base64pdf.txt";
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));

if (strpos($pdf_content, $str) !== false) {
echo 'true';
}


fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf, $pdf_decoded);
//close output file
fclose ($pdf);
echo 'Done';

Answer Source

The following will return everything after $str in the $pdf_content including $str.

if (strpos($pdf_content, $str) !== false) {
   $foundContent = substr($pdf_content, strpos($pdf_content, $str));
}

The strpos will return the numerical value of the start of the needle ($str) within the haystack ($pdf_content), which then can be pulled out uding substr.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download