PHP中解码混合HTML与Base64的内容,如何转为纯HTML?
Hey there! Looking at your example content, I notice those encoded segments (like =D7=94=D7=90=D7=9D) are actually using quoted-printable encoding (not Base64, which you mentioned). Quoted-printable is super common in email messages, which is why you see =3D replacing regular equals signs (since = is a reserved character in this encoding).
No worries though—PHP has built-in tools to handle this perfectly, and we can get you clean, valid HTML in no time.
1. Decode the Quoted-Printable Content
PHP's quoted_printable_decode() function is made exactly for this scenario. It will convert all those =XX hex sequences back to their original characters, while leaving your HTML tags completely intact.
Example Code:
// Your original mixed content $mixedContent = '<div dir=3D"ltr"> <div dir=3D"rtl">=D7=94=D7=90=D7=9D =D7=99=D7=A9 =D7=97=D7=93=D7=A9 =D7=91=D7=A0=D7=95=D7=A9=D7=90?<br clear=3D"all"></div> <div>DNS =D7=9B=D7=91=D7=A8 =D7=A2=D7=95=D7=93=D7=9B=D7=9F.</div> </div>'; // Decode the quoted-printable parts $cleanHtml = quoted_printable_decode($mixedContent); // Output the result echo $cleanHtml;
Resulting HTML:
<div dir="ltr"> <div dir="rtl">האם יש חדש בנושא?<br clear="all"></div> <div>DNS כבר עובד.</div> </div>
2. Optional: Clean Up and Validate HTML
If you want to ensure the final HTML is fully valid (like fixing any unquoted attributes that might slip through), you can use PHP's DOMDocument to parse and reformat the output:
$dom = new DOMDocument(); // Suppress minor HTML validation warnings (common for messy input) libxml_use_internal_errors(true); $dom->loadHTML($cleanHtml); libxml_clear_errors(); // Save the cleaned, well-formed HTML $finalHtml = $dom->saveHTML(); echo $finalHtml;
This will give you properly structured HTML with all attributes quoted and valid tag nesting.
Quick Notes:
- Quoted-Printable vs Base64: The key difference is that quoted-printable is designed for text with occasional non-ASCII characters (like your Hebrew text), using
=XXfor hex values. Base64 is for encoding binary data, so it wasn't the right fit here. - Character Encoding: Since your decoded text is Hebrew, make sure your PHP script outputs UTF-8 (add
header('Content-Type: text/html; charset=utf-8');if sending to a browser) to display everything correctly.
内容的提问来源于stack exchange,提问作者Whoopix

