【发布时间】: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 放到网上吗?