You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中解码混合HTML与Base64的内容,如何转为纯HTML?

Solution to Decode Mixed HTML and Quoted-Printable Content in PHP

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 =XX for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:15:01