Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中解析xml和输出编码_Php_Xml_Utf 8 - Fatal编程技术网

在php中解析xml和输出编码

在php中解析xml和输出编码,php,xml,utf-8,Php,Xml,Utf 8,我从一个XML文件在Wordpress中生成了很多帖子。令人担忧的是:重音字符 流的标题是: <? Xml version = "1.0" encoding = "ISO-8859-15"?> 以下是完整的流量: 我的网站是utf8 所以我使用函数utf8\u encode。。。但这并不能解决问题,口音总是被误解 有人有主意吗 编辑04-10-2011 18:02(法语时间): 以下是完整的流量: 这是我的密码: /** * parse an rss flux from ne

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

流的标题是:

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

以下是完整的流量:

我的网站是utf8

所以我使用函数utf8\u encode。。。但这并不能解决问题,口音总是被误解

有人有主意吗

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

以下是完整的流量:

这是我的密码:

/**
 * 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;

}
/**
*从netaffiliation解析rss流量,并将每个项目转换为帖子
*@var$flux=外部链接
*@returnbool
*/
专用函数解析FluxNetAffiliation($flux)
{
$content=file\u get\u contents($flux);
$content=iconv(“iso-8859-15”、“utf-8”、“$content”);
$xml=新文档;
$xml->loadXML($content);
//获取第一个链接:http://www.netaffiliation.com
$link=$xml->getElementsByTagName('link')->项(0);
//echo$link->textContent;
//我们获取所有项目并创建多维数组
$items=$xml->getElementsByTagName('item');
$offers=array();
//我们步行去买东西
foreach($items作为$item)
{
$childs=$item->childNodes;
//我们和孩子们一起散步
foreach($childs作为$child)
{
$offers[$child->nodeName][]=$child->nodeValue;
}
}
未设置($offers['#text']);
//我们为每个报价创建一篇文章
$nbrPosts=count($offers['title']);
如果($Nbr$offers['title'][$i],
“发布内容”=>$description,
“发布状态”=>“发布”,
“后作者”=>1,
'post_category'=>数组(self::getCatAffiliation())
);
//将帖子插入数据库
如果(!wp_insert_post($my_post));;
$i++;
}
echo self::getFeedback(“Le flux a généré{$nbrPosts}文章”对Le flux NetAffiliation dans la catégorie affiliation进行了“更新”);
返回false;
}

所有的帖子都生成了,但是。。。口音很难看。您可以在这里看到结果:

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


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

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

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

在不同编码之间切换时,有很多困难需要掌握。此外,使用多个字节对字符进行编码的编码(所谓的多字节编码),如WordPress使用的UTF-8,在PHP中值得特别注意

  • 首先,确保您创建的所有文件都以与将要提供的文件相同的编码保存。例如,确保您在“另存为…”对话框中设置的编码与在HTTP
    内容类型
    标题中使用的编码相同
  • 其次,您需要验证输入是否与要传递的文件具有相同的编码。在您的情况下,输入文件的编码为
    ISO-8859-15
    ,因此需要使用将其转换为
    UTF-8
  • 第三,您必须知道PHP本机不支持多字节编码,例如
    UTF-8
    。诸如
    htmlentities()
    之类的函数将生成奇怪的字符。对于这些函数中的许多函数,都有多字节可选,前缀为
    mb
    。如果您的编码是UTF-8,请检查您的文件中是否有此类函数,如有必要,请将其替换
有关这些主题的更多信息,请参阅,以及。

mb\u convert\u encoding()
拯救我的生命

以下是我的解决方案:

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

为什么需要生成
iso-8859-15
?流不是我的。流是在iso-8859-15,我想得到在UTF8的内容是干净的,在我的网站。嗯。LoadXML将解析XML头,因此iconv()在这里没有帮助。您可能需要强制执行正确的编码来加载XML,但我不知道如何。。。嗯,也许我应该(用preg_replace)将“编码”部分替换为“UTF8”或。。。一片空白。我会试试这个。它可能与
iconv
配合使用。。。你能把一个XML示例放到网上吗?重音会用你的代码转换成其他字符:s,谢谢anyway@Raphael你能展示一下你正在使用的代码和字符如何被破坏的例子吗?这种情况有点不清楚,请使用流链接和我的代码编辑我的答案。我必须回家,所以如果你回答我,我明天才能看到。谢谢你的帮助=)您好,我所有的文件都用UTF8编码(aptana中的默认选项)。我的元字符集是UTF8,我不使用htmlentities。无论如何,谢谢你的帮助这是一个很好的建议,但不是针对这种特殊情况