Algorithme de mangling d'url en Mysql
Par Mathieu Muller le vendredi, 17 avril 2009, 12:25 - Web - Lien permanent
Parfois, on doit pouvoir convertir des chaînes en UTF-8 en éliminant tous les caractères accentués, ou spéciaux. C'est notamment le cas quand on cherche à faire des urls SEO friendly (pas d'accents, tout en minuscule, élimination des espaces et autres tabulations dans du texte. La fonction stockée Mysql dans le billet du message se charge d'effectuer le sale boulot pour vous; Enjoy!
DELIMITER | DROP FUNCTION IF EXISTS MANGLE | CREATE FUNCTION MANGLE ( str VARCHAR(255) ) RETURNS VARCHAR(255) DETERMINISTIC NO SQL BEGIN SET str = REPLACE( str, "Š" COLLATE utf8_general_ci, "S" ); SET str = REPLACE( str, "Œ" COLLATE utf8_general_ci, "OE" ); SET str = REPLACE( str, "Ž" COLLATE utf8_general_ci, "Z" ); SET str = REPLACE( str, "š" COLLATE utf8_general_ci, "s" ); SET str = REPLACE( str, "œ" COLLATE utf8_general_ci, "oe" ); SET str = REPLACE( str, "ž" COLLATE utf8_general_ci, "z" ); SET str = REPLACE( str, "Ÿ" COLLATE utf8_general_ci, "Y" ); SET str = REPLACE( str, "¥" COLLATE utf8_general_ci, "Y" ); SET str = REPLACE( str, "µ" COLLATE utf8_general_ci, "u" ); SET str = REPLACE( str, "À" COLLATE utf8_general_ci, "A" ); SET str = REPLACE( str, "Á" COLLATE utf8_general_ci, "A" ); SET str = REPLACE( str, "Â" COLLATE utf8_general_ci, "A" ); SET str = REPLACE( str, "Ã" COLLATE utf8_general_ci, "A" ); SET str = REPLACE( str, "Ä" COLLATE utf8_general_ci, "A" ); SET str = REPLACE( str, "Å" COLLATE utf8_general_ci, "A" ); SET str = REPLACE( str, "Æ" COLLATE utf8_general_ci, "AE" ); SET str = REPLACE( str, "Ç" COLLATE utf8_general_ci, "C" ); SET str = REPLACE( str, "È" COLLATE utf8_general_ci, "E" ); SET str = REPLACE( str, "É" COLLATE utf8_general_ci, "E" ); SET str = REPLACE( str, "Ê" COLLATE utf8_general_ci, "E" ); SET str = REPLACE( str, "Ë" COLLATE utf8_general_ci, "E" ); SET str = REPLACE( str, "Ì" COLLATE utf8_general_ci, "I" ); SET str = REPLACE( str, "Í" COLLATE utf8_general_ci, "I" ); SET str = REPLACE( str, "Î" COLLATE utf8_general_ci, "I" ); SET str = REPLACE( str, "Ï" COLLATE utf8_general_ci, "I" ); SET str = REPLACE( str, "Ð" COLLATE utf8_general_ci, "D" ); SET str = REPLACE( str, "Ñ" COLLATE utf8_general_ci, "N" ); SET str = REPLACE( str, "Ò" COLLATE utf8_general_ci, "O" ); SET str = REPLACE( str, "Ó" COLLATE utf8_general_ci, "O" ); SET str = REPLACE( str, "Ô" COLLATE utf8_general_ci, "O" ); SET str = REPLACE( str, "Õ" COLLATE utf8_general_ci, "O" ); SET str = REPLACE( str, "Ö" COLLATE utf8_general_ci, "O" ); SET str = REPLACE( str, "Ø" COLLATE utf8_general_ci, "O" ); SET str = REPLACE( str, "Ù" COLLATE utf8_general_ci, "U" ); SET str = REPLACE( str, "Ú" COLLATE utf8_general_ci, "U" ); SET str = REPLACE( str, "Û" COLLATE utf8_general_ci, "U" ); SET str = REPLACE( str, "Ü" COLLATE utf8_general_ci, "U" ); SET str = REPLACE( str, "Ý" COLLATE utf8_general_ci, "Y" ); SET str = REPLACE( str, "ß" COLLATE utf8_general_ci, "ss" ); SET str = REPLACE( str, "à" COLLATE utf8_general_ci, "a" ); SET str = REPLACE( str, "á" COLLATE utf8_general_ci, "a" ); SET str = REPLACE( str, "â" COLLATE utf8_general_ci, "a" ); SET str = REPLACE( str, "ã" COLLATE utf8_general_ci, "a" ); SET str = REPLACE( str, "ä" COLLATE utf8_general_ci, "a" ); SET str = REPLACE( str, "å" COLLATE utf8_general_ci, "a" ); SET str = REPLACE( str, "æ" COLLATE utf8_general_ci, "ae" ); SET str = REPLACE( str, "ç" COLLATE utf8_general_ci, "c" ); SET str = REPLACE( str, "è" COLLATE utf8_general_ci, "e" ); SET str = REPLACE( str, "é" COLLATE utf8_general_ci, "e" ); SET str = REPLACE( str, "ê" COLLATE utf8_general_ci, "e" ); SET str = REPLACE( str, "ë" COLLATE utf8_general_ci, "e" ); SET str = REPLACE( str, "ì" COLLATE utf8_general_ci, "i" ); SET str = REPLACE( str, "í" COLLATE utf8_general_ci, "i" ); SET str = REPLACE( str, "î" COLLATE utf8_general_ci, "i" ); SET str = REPLACE( str, "ï" COLLATE utf8_general_ci, "i" ); SET str = REPLACE( str, "ð" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "ñ" COLLATE utf8_general_ci, "n" ); SET str = REPLACE( str, "ò" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "ó" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "ô" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "õ" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "ö" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "ø" COLLATE utf8_general_ci, "o" ); SET str = REPLACE( str, "ù" COLLATE utf8_general_ci, "u" ); SET str = REPLACE( str, "ú" COLLATE utf8_general_ci, "u" ); SET str = REPLACE( str, "û" COLLATE utf8_general_ci, "u" ); SET str = REPLACE( str, "ü" COLLATE utf8_general_ci, "u" ); SET str = REPLACE( str, "u" COLLATE utf8_general_ci, "u" ); SET str = REPLACE( str, "ý" COLLATE utf8_general_ci, "y" ); SET str = REPLACE( str, "ÿ" COLLATE utf8_general_ci, "y" ); SET str = REPLACE( str, "\"" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "\\" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "®" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "?" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "&" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "'" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "." COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "," COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "*" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "€" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "=" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "+" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, " " COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "----" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "---" COLLATE utf8_general_ci, "-" ); SET str = REPLACE( str, "--" COLLATE utf8_general_ci, "-" ); RETURN LCASE( TRIM( BOTH '-' FROM str ) ); END | DELIMITER ;
Commentaires
c est extremement facile mais faut un peu de reflexion quand meme