【问题标题】:parsing xml and output encoding in php在php中解析xml和输出编码
【发布时间】:2011-10-04 15:31:44
【问题描述】:

我在 Wordpress 中从一个 XML 文件生成了很多帖子。担心:重音字符。

流的头部是:

<? Xml version = "1.0" encoding = "ISO-8859-15"?>

这是完整的通量:http://flux.netaffiliation.com/rsscp.php?maff=177053821BA2E13E910D54

我的网站是 utf8 格式。

所以我使用函数 utf8_encode ...但这并不能解决问题,重音总是被误解。

有人有想法吗?

编辑 04-10-2011 18:02(法国时间):

这是完整的通量:http://flux.netaffiliation.com/rsscp.php?maff=177053821BA2E13E910D54

这是我的代码:

/**
 * parse an rss flux from netaffiliation and convert each item to posts
 * @var $flux = external link
 * @return bool
 */
private function parseFluxNetAffiliation($flux)
{
    $content = file_get_contents($flux);
    $content = iconv("iso-8859-15", "utf-8", $content);

    $xml = new DOMDocument;
    $xml->loadXML($content);

    //get the first link : http://www.netaffiliation.com
    $link = $xml->getElementsByTagName('link')->item(0);
    //echo $link->textContent;

    //we get all items and create a multidimentionnal array
    $items = $xml->getElementsByTagName('item');

    $offers = array();
    //we walk items
    foreach($items as $item)
    {
        $childs = $item->childNodes;

        //we walk childs
        foreach($childs as $child)
        {
            $offers[$child->nodeName][] = $child->nodeValue;
        }

    }
    unset($offers['#text']);

    //we create one article foreach offer
    $nbrPosts = count($offers['title']);

    if($nbrPosts <= 0) 
    {
        echo self::getFeedback("Le flux ne continent aucune offre",'error');
        return false;
    }

    $i = 0;
    while($i < $nbrPosts)
    {
        // Create post object
        $description = '<p>'.$offers['description'][$i].'</p><p><a href="'.$offers['link'][$i].'" target="_blank">'.$offers['link'][$i].'</a></p>';

        $my_post = array(
            'post_title' => $offers['title'][$i],
            'post_content' => $description,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_category' => array(self::getCatAffiliation())
        );

        // Insert the post into the database
        if(!wp_insert_post($my_post));;

        $i++;
    }

    echo self::getFeedback("Le flux a généré {$nbrPosts} article(s) depuis le flux NetAffiliation dans la catégorie affiliation",'updated');
    return false;

}

所有帖子都生成了,但是...重音字符很难看。你可以在这里看到结果:http://monsieur-mode.com/test/

【问题讨论】:

  • 为什么需要生成iso-8859-15
  • 流不是我的。流在 iso-8859-15 中,我想让 UTF8 中的内容在我的网站中保持干净。
  • 嗯。 LoadXML 将解析 XML 标头,因此 iconv() 在这里没有帮助。您可能必须对 LoadXML 强制执行正确的编码,但我不知道如何...嗯
  • 也许我应该用“UTF8”或......用空白替换(用preg_replace)“编码”部分。我试试这个。
  • 它可能与iconv 一起使用......你能把一个示例 XML 放到网上吗?

标签: php xml utf-8


【解决方案1】:

在不同编码之间进行交换时,您必须掌握很多困难。此外,像 WordPress 使用的 UTF-8 这样使用超过一个字节来编码字符的编码(所谓的多字节编码)在 PHP 中值得特别注意。

  • 首先,确保您创建的所有文件都使用与将要提供的相同编码进行保存。例如,请确保您设置的编码与“另存为...”对话框中的编码与您在 HTTP Content-Type 标头中使用的编码相同。
  • 其次,您需要验证输入与您要传递的文件具有相同的编码。在您的情况下,输入文件的编码为ISO-8859-15,因此您需要使用iconv() 将其转换为UTF-8
  • 第三,你必须知道PHP本身并不支持UTF-8这样的多字节编码。 htmlentities() 等函数会产生奇怪的字符。对于其中许多函数,有多字节替代方案,其前缀为mb_。如果您的编码是UTF-8,请检查您的文件是否有此类功能,并在必要时替换它们。

有关这些主题的更多信息,请参阅Wikipedia about variable-width encodingspage in the PHP-Manual

【讨论】:

  • 您好,我所有的文件都以 UTF8 编码(aptana 中的默认选项)。我的元字符集是 UTF8,我不使用 htmlentities。无论如何感谢您的帮助
  • 这是非常好的建议,但不适用于这种特定情况
【解决方案2】:

默认情况下,大多数应用程序使用 UTF-8 数据并输出 UTF-8 内容。 Wordpress 绝对不应该分开,并且肯定可以在 UTF-8 基础上工作。

我根本不会在打印时转换任何信息,而是将您的标头更改为 UTF-8 而不是 ISO-8859-15。

【讨论】:

    【解决方案3】:

    如果您传入的 XML 数据是 ISO-8859-15,请使用iconv() 进行转换:

    $stream = file_get_contents("stream.xml");
    $stream = iconv("iso-8859-15", "utf-8", $stream);
    

    【讨论】:

    • 用您的代码将口音转换为其他字符:s,无论如何,谢谢您
    • @Raphael 您能否展示您正在使用的代码以及字符如何破坏的示例?情况有点不清楚
    • 我使用流链接和我的代码编辑我的答案。我必须去我家,所以如果你回答我,我明天才能看到。感谢您的帮助 =)
    【解决方案4】:

    mb_convert_encoding()救了我的命。

    这是我的解决方案:

        $content = preg_replace('/ encoding="ISO-8859-15"/is','',$content);
        $content = mb_convert_encoding($content,"UTF-8");
    

    【讨论】:

      猜你喜欢
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多