【发布时间】:2015-06-18 16:17:01
【问题描述】:
我正在寻找一种方法,可以在 MIME 编码的电子邮件中简单地将字符替换为对应的 ASCII 字符。我已经在下面编写了初步代码,但似乎我正在使用的 str_replace 命令将继续运行以捕获所有可能的组合。有没有更有效的方法来做到这一点?
<?php
$strings = "=?utf-8?Q?UK=20Defence=20=2D=20Yes=2C=20Both=20Labour=20and=20Tory=20Need=20To=20Be=20Very=20Much=20Clearer=20On=20Defence?=";
function decodeString($input){
$space = array("=?utf-8?Q?","=?UTF-8?Q?", "=20","?=");
$hyphen = array("=E2=80=93","=2D");
$dotdotdot = "=E2=80=A6";
$pound = "=C2=A3";
$comma = "=2C";
$decode = str_replace($space, ' ', $input);
$decode = str_replace($hyphen, '-', $decode);
$decode = str_replace($pound, '£', $decode);
$decode = str_replace($comma, ',', $decode);
$decode = str_replace($dotdotdot, '...', $decode);
return $decode;
}
echo decodeString($strings);
?>
【问题讨论】:
-
你能定义“ASCII对应”吗...?
-
好吧,就像 =20 是一个空格等等。反正我已经想通了,请看下面的答案。
-
那么供将来参考:这些电子邮件不是UTF-8 编码,而是MIME 编码。 UTF-8 本身与此无关。
-
是的,意识到这一点让我得到了我需要的答案。谢谢:)