我正在创建一个脚本来将旧网站迁移到 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);
关于php - 找不到在 joomla db 中插入文章的良好编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186765/