Florin Chiş

MySQL snippets, tips and tricks

  • Home
  • MySQL Tips
  • Quiz time ?
  • MySQL Tools
  • About me
RSS

Convert UTF8 diacritics to ASCII in MySQL for use in website links

Posted on 28/12/2011 by admin
No commentsLeave a comment

A very common problem for web developers is to maintain a normalized link (a string to identify a page) for their webpages when that string is from a foreign language that contains diacritic. For example, the string “ştiaţi că…” (in Romanian means “did you know”), should be converted to “stiati ca…”, so romanian diacritics are converted to it’s latin representation. That means that “ă” becomes “a”, “ş” becomes “s” and “ţ” becomes “t”. This transformation is useful because some search engines and some browsers don’t get well with these diacritics.
There is a mysql stored routine to convert such characters. That procedure uses a mysql table that contain about 400 characters and their latin representation. This is a mapping table between different alphabets and latin alphabet. The table has been created based on standard mysql charsets (see Collation chart for utf8_general_ci, European alphabets). Procedure listing is shown below. There is a text file with SQL queries to create utf8_translation and it’s content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
CREATE FUNCTION `util_utf8_to_ascii`(`_str` VARCHAR(255))
    RETURNS varchar(255)
    LANGUAGE SQL
    SQL SECURITY INVOKER
    COMMENT 'Convert UTF8 to ASCII based on utf8_translation'
BEGIN
    DECLARE cutf8 VARCHAR(5);
    DECLARE cascii CHAR(1);
    DECLARE newStr VARCHAR(255);
    DECLARE done INT DEFAULT 0;
    DECLARE cur1 CURSOR FOR SELECT u.c_utf8, u.c_ascii FROM utf8_translation AS u;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    OPEN cur1;
    SET newStr = _str;
    read_loop: LOOP
        FETCH cur1 INTO cutf8, cascii;
        IF done THEN
            LEAVE read_loop;
        END IF;
        IF (LOCATE(cutf8, _str)) THEN
            SET newStr = REPLACE(newStr, cutf8, cascii);
        END IF;
    END LOOP;
    RETURN newStr;
END
Categories: MySQL small projects | Tags: conversion, latin, link, procedure, translation, utf8

About admin

Born in 1985 in Baia Mare, Maramures county in Romania. Studied at North University of Baia Mare, and obtained bachelor degree in 2011 in Computational Mathematic Applied in Informatics.
View all posts by admin→
Generate table with random text from dictionary

Lasă un răspuns Anulează răspuns

Adresa ta de email nu va fi publicată. Câmpurile necesare sunt marcate *

*

*


question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

Poți folosi aceste etichete HTML și atribute: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Search

  • Recent posts

    • Convert UTF8 diacritics to ASCII in MySQL for use in website links
    • Generate table with random text from dictionary
    • Test
    • Repair (or optimize) all tables from a database
    • Padding a string with a char to complete fixed length width
  • What about you ?

    Do you create stored routines in MySQL ?

    View Results

    Loading ... Loading ...
    • Polls Archive
  • Categories

    • General
    • MySQL small projects
    • Mysql stored routines
  • Newsletter

    Want to receive articles in your mailbox ?

  • Meteo

    meteoblue.com
  • Legături

    • Cosmin Roman blog
    • Romania Maps
  • Tags

    article automatic char conversion database dictionary fixed generate latin length link mysql padding procedure random repair script tables text translation utf8 word

    WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

  • Archive

    • decembrie 2011
  • New comments

    • Parteneri

      Bloguri, Bloggeri si Cititori
      Blogarama - The Blog Directory
    Mysql tips, tricks and tests. Proudly Powered by WordPress | Nest Theme by YChong