我有代码:
mysql_connect('...');
mysql_select_db('...');
$result = mysql_query('SELECT `oldword`, `newword` FROM `#_words`');
$find = array();
$replace = array();
while ($row = mysql_fetch_assoc($result)) {
$find[] = $row['oldword'];
$replace[] = $row['newword'];
}
$oldstring = 'Some text';
$newstring = str_replace($find, $replace, $oldstring);
echo $newstring;
它工作得很好,但有两个问题。首先,我需要将其转换为使用 Joomla API 即类似于
$db = &JFactory::getDBO();
$db->setQuery('SELECT `oldword`, `newword` FROM `#_words`');
// and what is next, what about mysql_fetch_assoc ?
其次,如果 oldword 和 newword 是英文的,它可以工作,但如果是西里尔文,它就不行。我该如何解决?我试过这个:
function fixEncoding($s, $encoding = 'UTF-8') {
$s = @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s));
return str_replace("\xEF\xBB\xBF", '', $s);
}
$find = fixEncoding($find);
$replace = fixEncoding($replace);
但此函数仅适用于单个字符串,不适用于数组