我正在尝试使用 Zend_Cache 保存 UTF-8 字符(如 Ť、š 等),但 Zend_Cache 将它们弄乱了并将它们保存为 Å、¾ 和其他奇怪的字符。
这是我将数据保存到缓存中的代码片段(UTF-8 字符仅在网上被弄乱了,当我在本地主机上的 PC 上尝试它时它可以正常工作):
// cache the external data
$data = array('nextRound' => $nextRound,
'nextMatches' => $nextMatches,
'leagueTable' => $leagueTable);
$cache = Zend_Registry::get('cache');
$cache->save($data, 'externalData');
在保存缓存数据之前,我使用 HTMLPurifier 对其进行净化,并使用 DOM 进行一些解析,如下所示:
// fetch the HTML from external server
$html = file_get_contents('http://www.example.com/test.html');
// purify the HTML so we can load it with DOM
include BASE_PATH . '/library/My/htmlpurifier-4.0.0-standalone/HTMLPurifier.standalone.php';
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Doctype', 'XHTML 1.0 Strict');
$purifier = new HTMLPurifier($config);
$html = $purifier->purify($html);
$dom = new DOMDocument();
// hack to preserver UTF-8 characters
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
$dom->preserveWhiteSpace = false;
// some parsing here
下面是我在引导文件中初始化 Zend_Cache 的方法:
protected function _initCache()
{
$frontend= array('lifetime' => 7200,
'automatic_serialization' => true);
$backend= array('cache_dir' => 'cache');
$this->cache = Zend_Cache::factory('core',
'File',
$frontend,
$backend);
}
有任何想法吗?它适用于本地主机(我支持 HTML 中使用的外语),但不适用于服务器。