5

アクセント付きのutf8文字をASCII文字列に変換するために、phpのiconvに似たmysqlの関数はありますか?

次のようなSQLクエリが必要です。

SELECT some_func("LĄBĄS VĄKĄRŪŠ")

そして戻るlabas vakarus

4

3 に答える 3

3

あなたが探しているかもしれないこのMySQL音訳関数を見つけました。

詳細については、関連する著者のブログ投稿を参照してください。

アドバイス:

私のアドバイスは、MySQL と PHP の両方の文字変換を信頼iconv()して同じ結果を生成しないことです。どちらも当て推量です。MySQL 側または PHP 側のみのいずれか 1 つに固執するのが最善です。

于 2012-07-28T10:07:09.863 に答える
2

MySQL で検索エンジンに適した URL アドレスを作成する機能。

DELIMITER ;;

CREATE FUNCTION `cool_url` (`original` varchar(512) CHARSET utf8mb4 COLLATE utf8mb4_bin) RETURNS varchar(512) CHARSET utf8mb4
BEGIN

    DECLARE translit VARCHAR(512) DEFAULT '';
    DECLARE len INT(3) DEFAULT 0;
    DECLARE pos INT(3) DEFAULT 1;
    DECLARE letter VARCHAR(2);

    SET original = TRIM(LOWER(original));
    SET len = CHAR_LENGTH(original);

    WHILE (pos <= len) DO

        # get one letter from original
        SET letter = SUBSTRING(original, pos, 1) COLLATE utf8mb4_bin;

        CASE TRUE
          # basic chars
          WHEN letter IN ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9' COLLATE utf8mb4_bin) THEN SET letter = letter; 
          # chars with diacritics and azbuka
          WHEN letter IN('á','à','â','ä','å','ā','ą','ă' COLLATE utf8mb4_bin) THEN SET letter = 'a';
          WHEN letter IN('б' COLLATE utf8mb4_bin) THEN SET letter = 'b';
          WHEN letter IN('č','ć','ç','ć' COLLATE utf8mb4_bin) THEN SET letter = 'c';
          WHEN letter IN('ď','đ','д' COLLATE utf8mb4_bin) THEN SET letter = 'd';
          WHEN letter IN('é','ě','ë','ё','è','ê','ē','ę','ь','э','œ' COLLATE utf8mb4_bin) THEN SET letter = 'e';
          WHEN letter IN('ф','ѳ' COLLATE utf8mb4_bin) THEN SET letter = 'f';
          WHEN letter IN('ģ','ğ','г' COLLATE utf8mb4_bin) THEN SET letter = 'g';
          WHEN letter IN('í','î','ï','ī','î','і','и','й' COLLATE utf8mb4_bin) THEN SET letter = 'i';
          WHEN letter IN('ķ','к' COLLATE utf8mb4_bin) THEN SET letter = 'k';
          WHEN letter IN('ľ','ĺ','ļ','ł','л' COLLATE utf8mb4_bin) THEN SET letter = 'l';
          WHEN letter IN('м' COLLATE utf8mb4_bin) THEN SET letter = 'm';
          WHEN letter IN('ň','ņ','ń','ñ','н' COLLATE utf8mb4_bin) THEN SET letter = 'n';
          WHEN letter IN('ó','ö','ø','õ','ô','ő','ơ','о' COLLATE utf8mb4_bin) THEN SET letter = 'o';
          WHEN letter IN('п' COLLATE utf8mb4_bin) THEN SET letter = 'p';
          WHEN letter IN('ŕ','ř','р' COLLATE utf8mb4_bin) THEN SET letter = 'r';
          WHEN letter IN('š','ś','ș','ş','с','ß' COLLATE utf8mb4_bin) THEN SET letter = 's';
          WHEN letter IN('ť','ț','т' COLLATE utf8mb4_bin) THEN SET letter = 't';
          WHEN letter IN('ú','ů','ü','ù','û','ū','ű','ư','у' COLLATE utf8mb4_bin) THEN SET letter = 'u';
          WHEN letter IN('в' COLLATE utf8mb4_bin) THEN SET letter = 'v';
          WHEN letter IN('ý','ы','ѵ' COLLATE utf8mb4_bin) THEN SET letter = 'y';
          WHEN letter IN('ž','ź','ż','з' COLLATE utf8mb4_bin) THEN SET letter = 'z';
          WHEN letter = 'х' COLLATE utf8mb4_bin THEN SET letter = 'kh'; -- is not X
          WHEN letter = 'ю' COLLATE utf8mb4_bin THEN SET letter = 'ju';
          WHEN letter = 'я' COLLATE utf8mb4_bin THEN SET letter = 'ja';
          WHEN letter = 'ж' COLLATE utf8mb4_bin THEN SET letter = 'zh';
          WHEN letter = 'ч' COLLATE utf8mb4_bin THEN SET letter = 'ch';
          WHEN letter = 'ш' COLLATE utf8mb4_bin THEN SET letter = 'sh';
          WHEN letter = 'щ' COLLATE utf8mb4_bin THEN SET letter = 'shch';
          WHEN letter = 'ѣ' COLLATE utf8mb4_bin THEN SET letter = 'ie';
          WHEN letter = 'ц' COLLATE utf8mb4_bin THEN SET letter = 'ts';
          WHEN letter = 'ъ' COLLATE utf8mb4_bin THEN SET letter = ''; -- hard sign
          WHEN letter = 'ь' COLLATE utf8mb4_bin THEN SET letter = ''; -- soft sign

          ELSE SET letter = '-';
        END CASE;

        # joining new string
        SET translit = CONCAT(translit, letter);

        SET pos = pos + 1;
    END WHILE;

    # replacing more dashes by one
    WHILE (translit REGEXP '\-{2,}') DO
        SET translit = REPLACE(translit, '--', '-');
    END WHILE;

    RETURN TRIM(BOTH '-' FROM translit);

END;;
DELIMITER ;

例 (テスト):

mysql> SELECT cool_url('BŒautiful day áàâäåāąă б čćçć ďđд éěëёèêēęьэœ фѳ ģğг íîïīîіий ķк ľĺļłл м ňņńñн óöøõôőơо п ŕřр šśșşсß ťțт úůüùûūűưу в ýыѵ žźżз х ю я ж ч ш щ ѣ ц ъ ь');
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| beautiful-day-aaaaaaaa-b-cccc-ddd-eeeeeeeeeee-ff-ggg-iiiiiiii-kk-lllll-m-nnnnn-oooooooo-p-rrr-ssssss-ttt-uuuuuuuuu-v-yyy-zzzz-kh-ju-ja-zh-ch-sh-sh-ie-ts-e                  |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
于 2014-03-24T19:49:58.673 に答える
1

wp で投稿タグ用の数千のスラッグを作成するには、汚いが迅速な解決策が必要でした。これは、母音と数字用に修正した上記の回答から取得したコードです。今日私を助けるように、いつか誰かを助けることを願っています。:)

DELIMITER $$

DROP FUNCTION IF EXISTS `transliterate` $$
CREATE FUNCTION `transliterate` (original VARCHAR(512)) RETURNS VARCHAR(512)
BEGIN

  DECLARE translit VARCHAR(512) DEFAULT '';
  DECLARE len INT(3) DEFAULT 0;
  DECLARE pos INT(3) DEFAULT 1;
  DECLARE letter CHAR(2);

  SET original = TRIM(LOWER(original));
  SET len = CHAR_LENGTH(original);

  WHILE (pos <= len) DO
    SET letter = SUBSTRING(original, pos, 1);

    CASE TRUE

      WHEN letter IN('á','à','â','ä','å','ā','ą','ă') THEN SET letter = 'a';
      WHEN letter IN('č','ć','ç','ć') THEN SET letter = 'c';
      WHEN letter IN('ď','đ') THEN SET letter = 'd';
      WHEN letter IN('é','ě','ë','è','ê','ē','ę') THEN SET letter = 'e';
      WHEN letter IN('ģ','ğ') THEN SET letter = 'g';
      WHEN letter IN('í','î','ï','ī','î') THEN SET letter = 'i';
      WHEN letter IN('ķ') THEN SET letter = 'k';
      WHEN letter IN('ľ','ĺ','ļ','ł') THEN SET letter = 'l';
      WHEN letter IN('ň','ņ','ń','ñ') THEN SET letter = 'n';
      WHEN letter IN('ó','ö','ø','õ','ô','ő','ơ') THEN SET letter = 'o';
      WHEN letter IN('ŕ','ř') THEN SET letter = 'r';
      WHEN letter IN('š','ś','ș','ş') THEN SET letter = 's';
      WHEN letter IN('ť','ț') THEN SET letter = 't';
      WHEN letter IN('ú','ů','ü','ù','û','ū','ű','ư') THEN SET letter = 'u';
      WHEN letter IN('ý') THEN SET letter = 'y';
      WHEN letter IN('ž','ź','ż') THEN SET letter = 'z';

    WHEN letter = 'а' THEN SET letter = 'a';
      WHEN letter = 'б' THEN SET letter = 'b';
      WHEN letter = 'в' THEN SET letter = 'v';
      WHEN letter = 'г' THEN SET letter = 'g';
      WHEN letter = 'д' THEN SET letter = 'd';
    WHEN letter = 'е' THEN SET letter = 'e';
    WHEN letter = 'ё' THEN SET letter = 'e';      
      WHEN letter = 'ж' THEN SET letter = 'z';
      WHEN letter = 'з' THEN SET letter = 'z';
      WHEN letter = 'и' THEN SET letter = 'i';
      WHEN letter = 'й' THEN SET letter = 'i';
      WHEN letter = 'к' THEN SET letter = 'k';
      WHEN letter = 'л' THEN SET letter = 'l';
      WHEN letter = 'м' THEN SET letter = 'm';
      WHEN letter = 'н' THEN SET letter = 'n';
    WHEN letter = 'о' THEN SET letter = 'o';
      WHEN letter = 'п' THEN SET letter = 'p';
    WHEN letter = 'р' THEN SET letter = 'r';
    WHEN letter = 'с' THEN SET letter = 's';
      WHEN letter = 'т' THEN SET letter = 't';
    WHEN letter = 'у' THEN SET letter = 'u';
      WHEN letter = 'ф' THEN SET letter = 'f';
      WHEN letter = 'х' THEN SET letter = 'ch';
      WHEN letter = 'ц' THEN SET letter = 'c';
      WHEN letter = 'ч' THEN SET letter = 'c';
      WHEN letter = 'ш' THEN SET letter = 's';
      WHEN letter = 'щ' THEN SET letter = 's';
      WHEN letter = 'ъ' THEN SET letter = '';
      WHEN letter = 'ы' THEN SET letter = 'y';
      WHEN letter = 'ь' THEN SET letter = 'e';
      WHEN letter = 'э' THEN SET letter = 'e';
      WHEN letter = 'ю' THEN SET letter = 'ju';
      WHEN letter = 'я' THEN SET letter = 'ja';

    WHEN letter = '0' THEN SET letter = '0';
    WHEN letter = '1' THEN SET letter = '1';
    WHEN letter = '2' THEN SET letter = '2';
    WHEN letter = '3' THEN SET letter = '3';
    WHEN letter = '4' THEN SET letter = '4';
    WHEN letter = '5' THEN SET letter = '5';
    WHEN letter = '6' THEN SET letter = '6';      
    WHEN letter = '7' THEN SET letter = '7';
    WHEN letter = '8' THEN SET letter = '8';
    WHEN letter = '9' THEN SET letter = '9';



      WHEN letter IN ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','z')
        THEN SET letter = letter;

      ELSE
    SET letter = '-';

    END CASE;

    SET translit = CONCAT(translit, letter);
    SET pos = pos + 1;
  END WHILE;

  WHILE (translit REGEXP '\-{2,}') DO
        SET translit = REPLACE(translit, '--', '-');
  END WHILE;

  RETURN TRIM(BOTH '-' FROM translit);

END $$

DELIMITER ;
于 2017-01-13T13:49:06.720 に答える