【问题标题】:Zend_Cache And UTF-8 ProblemZend_Cache 和 UTF-8 问题
【发布时间】:2010-02-10 00:35:07
【问题描述】:

我正在尝试使用 Zend_Cache 保存 UTF-8 字符(如 Ť、š 等),但 Zend_Cache 将它们弄乱了并将它们保存为 Å、¾ 和其他奇怪的字符。

这是我的代码的 sn-p,它将数据保存到缓存中(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 中使用的外语),但不适用于服务器。

【问题讨论】:

  • 您确定不是净化器处理造成的吗?如果您消除该过程的那部分会发生什么?
  • 我有 99% 的把握。我尝试消除该过程的 HTMLPurifier 部分,但问题仍然存在。
  • 请在本地主机和服务器上的 php.ini 中发布您的 mbstring 配置
  • 嗨,米哈尔。我实际上发现是 DOM 弄乱了 UTF-8 字符,请参阅这个问题:stackoverflow.com/questions/2236889/…

标签: php zend-framework utf-8 zend-cache


【解决方案1】:

我在部署 FPDF 时遇到了类似的问题。在这里,html 空格字符 &nbsp 被转换为与您在这里相同的 Å 字符。在我的本地 Windows 上很好,但在我的 linux 服务器环境中不起作用。

试试这个:

$str = iconv('UTF-8', 'windows-1252', html_entity_decode($str));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    相关资源
    最近更新 更多