我正在创建一个脚本来将旧网站迁移到joomla中。 在我的旧网站上,所有的php脚本都是utf8。导入脚本也是。
为了创建文章,我这样做:
$article = JTable::getInstance('content');
$article->title = $titre;
$article->alias = $alias;
$article->introtext = $contenu;
$article->catid = $idcat;
$article->created = JFactory::getDate()->toSQL();;
$article->created_by_alias = 'Import';
$article->state = 1;
$article->access = 1;
$article->metadesc = $description;
$article->metadata = '{"page_title":"'.$titre.'","author":"","robots":""}';
$article->language = '*';
if (!$article->check())
print $article->getError();
if (!$article->store(TRUE))
print $article->getError();
在那之后一切都很好,但我有第二遍可以做到这一点:
$query = $db->getQuery(true);
$query->select("id,introtext");
$query->from("#__content");
$query->where("1");
$db->setQuery((string) $query);
$messages = $db->loadObjectList();
foreach($messages as $page)
{
$idarticle=$page->id;
$dom = new DOMDocument;
@$dom->loadHTML(utf8_decode($page->introtext));
...
$fields = array("introtext=".$db->quote(utf8_encode($dom->saveHTML())));
$conditions = array("id='$idarticle'");
$query->update('#__content')->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
我尝试过有或没有utf8_decode
/utf8_encode
,它是相同的:例如,有些字符被替换为?
’
但重音字符很好。
我找到了一个解决方案,不确定它是否会导致问题: 在创建文章之前,以这种方式转换特殊字符:
$html=htmlentities($html,ENT_NOQUOTES|ENT_SUBSTITUTE|ENT_DISALLOWED);
$html=str_replace("<","<",$html);
$html=str_replace(">",">",$html);