我正在创建一个脚本,将一个旧网站迁移到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 这是一样的:一些字符被替换为 ? 例如 â 但口音很好。
utf8_decode
utf8_encode
?
â
我找到了一个解决方案,不确定它是否会导致问题: 在创建文章之前,请通过以下方式转换特殊字符:
$html=htmlentities($html,ENT_NOQUOTES|ENT_SUBSTITUTE|ENT_DISALLOWED); $html=str_replace("<","<",$html); $html=str_replace(">",">",$html);