在MySQL中将字符转换为UTF8(éto é)

a2mppw5e  于 2023-04-04  发布在  Mysql
关注(0)|答案(3)|浏览(135)

我有一个MySQL的问题。我的整个网站,除了我的数据库是在UTF8。我只是最近试图转换它到UTF8与各种命令,但主要问题仍然存在:
重音不起作用。我用é代替é。è也是一样,还有另一种奇怪的字符。
是否有一个简单的转换所有这些字符,让我们说,到é,在基地?
多谢了。
(by顺便说一句,数据库是拉丁语的1)

pengsaosao

pengsaosao1#

在本例中,éé,解释为LATIN 1并转换为UTF-8。可能是您的数据库试图提供帮助并转换为UTF-8,而无需转换。
你试过切换数据库表的字符集吗?

jbose2ul

jbose2ul2#

你可以试试这个功能:

CREATE TABLE `utf8decodemap` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `encoded` VARCHAR(128) NOT NULL,
  `decoded` VARCHAR(128) NOT NULL,
  UNIQUE KEY urlcodemapUIdx1(encoded),
  PRIMARY KEY (`id`)  
);

INSERT INTO utf8decodemap (decoded,encoded) VALUES ("â","â");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("é","é");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("è","è");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ê","ê");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ë","ë");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("î","î");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ï","ï");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ô","ô");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ö","ö");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ù","ù");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("û","û");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ü","ü");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("ç","ç");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("œ","Å?");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("€","â¬");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("°","°");
INSERT INTO utf8decodemap (decoded,encoded) VALUES ("à","Ã");

DELIMITER $$
CREATE FUNCTION `utf8decoder`(str VARCHAR(4096)) RETURNS VARCHAR(4096) DETERMINISTIC
BEGIN
               DECLARE X  INT;               
               DECLARE chr VARCHAR(256);
               DECLARE chrto VARCHAR(256);
               DECLARE result VARCHAR(4096);
               SET X = 1;
               WHILE X  <= (SELECT MAX(id) FROM utf8decodemap) DO
                   SET chr = (SELECT `encoded` FROM utf8decodemap WHERE id = X);
                   SET chrto = (SELECT `decoded` FROM utf8decodemap WHERE id = X);                
                           SET str = REPLACE(str,chr,chrto);
                           SET  X = X + 1;                           
               END WHILE;
               RETURN str;
       END$$

DELIMITER ;

例如:从MYtable中选择utf8decoder(MyCol)

rsaldnfx

rsaldnfx3#

感谢上面的列表,也许这个PHP脚本可以帮助一些人
1-溶液A

$title = mb_convert_encoding($article['post_title'], 'ISO-8859-1', 'UTF-8');

2-溶液B

function convertSpacialChars($text)
{
    $charsMap = [
        "â" => "â",
        "é" => "é",
        "è" => "è",
        "ê" => "ê",
        "ë" => "ë",
        "î" => "î",
        "ï" => "ï",
        "ô" => "ô",
        "ö" => "ö",
        "ù" => "ù",
        "û" => "û",
        "ü" => "ü",
        "ç" => "ç",
        "Å?" => "œ",
        "â¬" => "€",
        "°" => "°",
        "Ã" => "à",
    ];
    return str_replace(array_keys($charsMap),array_values($charsMap),$text);
}

相关问题