我试图通过PHP中的URL通过Exchange 2003读取电子邮件。文件名中不允许字符的文件将它们转换为某种形式的Unicode。对于例如,g/转换为xF8FF,并将\转换为xF8FE
如何使用PHP将这些字符转换为正确的编码?我知道我可以在很长一段时间内使用str_replace,但我知道其他字符(如:;*<>)也会遇到同样的问题。PHP本机支持这种编码吗?
谢谢
发布于 2012-08-02 19:30:04
从(用C编程语言编写)的源代码开始,我编写了以下示例:
<?php
class myExchange {
private $uri_encoded_char = array(
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00 - 0x0f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x10 - 0x1f */
1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, /* ' ' - '/' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 2, /* '0' - '?' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* '@' - 'O' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 1, 0, /* 'P' - '_' */
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* '`' - 'o' */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 1, /* 'p' - 0x7f */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
);
/**
* e2k_uri_append_encoded:
*
* Appends $in to $str, encoding URI-unsafe characters as needed
* (optionally including some Exchange-specific encodings).
* When appending a path, you must append each segment separately;
* e2k_uri_append_encoded() will encode any "/"s passed in.
*
* @param string $str a string containing part of a URI
* @param string $in data to append to $str
* @param bool $wss_encode whether or not to use the
* special Web Storage System
* encoding rules
* @param string $extra_enc_chars additional characters beyond
* the normal URI-reserved
* characters to encode when
* appending to $str
* @return string
**/
public function e2k_uri_append_encoded($str, $in, $wss_encode, $extra_enc_chars) {
$len = strlen($in);
for ($i = 0; $i < $len; $i++) {
$s = $in[$i];
$c = ord($s);
if ($extra_enc_chars && strchr($extra_enc_chars, $s)) {
$str .= sprintf("%%%02x", $c);
} else {
switch ($this->uri_encoded_char[$c]) {
case 2:
if (!$wss_encode) {
$str .= sprintf("%%%02x", $c);
} else {
switch ($s) {
case '/':
$str .= "_xF8FF_";
break;
case '?':
$str .= "_x003F_";
break;
case '\\':
$str .= "_xF8FE_";
break;
case '~':
$str .= "_x007E_";
break;
}
}
break;
case 1:
$str .= sprintf("%%%02x", $c);
break;
default:
$str .= $s;
break;
}
}
}
return($str);
}
}
$filename = "@#£¤$%&/{([)]=}+?'`|~,;.:-_<>æøåäâãëêïîöôõüûÿ\\.EML";
$e = new myExchange();
echo $e->e2k_uri_append_encoded("", $filename, true, null);
echo "\n";
?>这是输出:
@%23%a3%a4$%25%26_xF8FF_%7b(%5b)%5d=%7d+_x003F_'%60%7c_x007E_,;.:-_%3c%3e%e6%f8%e5%e4%e2%e3%eb%ea%ef%ee%f6%f4%f5%fc%fb%ff_xF8FE_.EML不幸的是,我没有Exchange,所以我无法判断它是否真正有效,但是我希望它是一个很好的起点。
发布于 2012-08-02 17:56:19
尼克,你应该看看这个问题:MSExchange URL编码
OP与您有相同的问题,其中一个答案提供了一些关于如何进行转换的技巧。
https://stackoverflow.com/questions/11662095
复制相似问题