Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created August 28, 2017 22:02
Show Gist options
  • Save joshuaadickerson/e5b318e964f1694dbb100cb81941e06b to your computer and use it in GitHub Desktop.
Save joshuaadickerson/e5b318e964f1694dbb100cb81941e06b to your computer and use it in GitHub Desktop.
DELIMITER $$
DROP FUNCTION IF EXISTS `HTML_ENCODE`$$
CREATE FUNCTION `HTML_ENCODE`(X TEXT) RETURNS TEXT CHARSET latin1 DETERMINISTIC
BEGIN
DECLARE TextString TEXT;
SET TextString = X ;
#quotation mark
IF INSTR( X , '"' )
THEN SET TextString = REPLACE(TextString, '"', '"') ;
END IF ;
#apostrophe
IF INSTR( X , '"' )
THEN SET TextString = REPLACE(TextString, '"', ''') ;
END IF ;
#ampersand
IF INSTR( X , '&' )
THEN SET TextString = REPLACE(TextString, '&', '&') ;
END IF ;
#less-than
IF INSTR( X , '<' )
THEN SET TextString = REPLACE(TextString, '<', '&lt;') ;
END IF ;
#greater-than
IF INSTR( X , '>' )
THEN SET TextString = REPLACE(TextString, '>', '&gt;') ;
END IF ;
#non-breaking space
IF INSTR( X , ' ' )
THEN SET TextString = REPLACE(TextString, ' ', ' &nbsp;') ;
END IF ;
#inverted exclamation mark
IF INSTR( X , '¡' )
THEN SET TextString = REPLACE(TextString, '¡', '&iexcl;') ;
END IF ;
#cent
IF INSTR( X , '¢' )
THEN SET TextString = REPLACE(TextString, '¢', '&cent;') ;
END IF ;
#pound
IF INSTR( X , '£' )
THEN SET TextString = REPLACE(TextString, '£', '&pound;') ;
END IF ;
#currency
IF INSTR( X , '¤' )
THEN SET TextString = REPLACE(TextString, '¤', '&curren;') ;
END IF ;
#yen
IF INSTR( X , '¥' )
THEN SET TextString = REPLACE(TextString, '¥', '&yen;') ;
END IF ;
#broken vertical bar
IF INSTR( X , '¦' )
THEN SET TextString = REPLACE(TextString, '¦', '&brvbar;') ;
END IF ;
#section
IF INSTR( X , '§' )
THEN SET TextString = REPLACE(TextString, '§', '&sect;') ;
END IF ;
#spacing diaeresis
IF INSTR( X , '¨' )
THEN SET TextString = REPLACE(TextString, '¨', '&uml;') ;
END IF ;
#copyright
IF INSTR( X , '©' )
THEN SET TextString = REPLACE(TextString, '©', '&copy;') ;
END IF ;
#feminine ordinal indicator
IF INSTR( X , 'ª' )
THEN SET TextString = REPLACE(TextString, 'ª', '&ordf;') ;
END IF ;
#angle quotation mark (left)
IF INSTR( X , '«' )
THEN SET TextString = REPLACE(TextString, '«', '&laquo;') ;
END IF ;
#negation
IF INSTR( X , '¬' )
THEN SET TextString = REPLACE(TextString, '¬', '&not;') ;
END IF ;
#soft hyphen
IF INSTR( X , '­' )
THEN SET TextString = REPLACE(TextString, '­', '&shy;') ;
END IF ;
#registered trademark
IF INSTR( X , '®' )
THEN SET TextString = REPLACE(TextString, '®', '&reg;') ;
END IF ;
#spacing macron
IF INSTR( X , '¯' )
THEN SET TextString = REPLACE(TextString, '¯', '&macr;') ;
END IF ;
#degree
IF INSTR( X , '°' )
THEN SET TextString = REPLACE(TextString, '°', '&deg;') ;
END IF ;
#plus-or-minus
IF INSTR( X , '±' )
THEN SET TextString = REPLACE(TextString, '±', '&plusmn;') ;
END IF ;
#superscript 2
IF INSTR( X , '²' )
THEN SET TextString = REPLACE(TextString, '²', '&sup2;') ;
END IF ;
#superscript 3
IF INSTR( X , '³' )
THEN SET TextString = REPLACE(TextString, '³', '&sup3;') ;
END IF ;
#spacing acute
IF INSTR( X , '´' )
THEN SET TextString = REPLACE(TextString, '´', '&acute;') ;
END IF ;
#micro
IF INSTR( X , 'µ' )
THEN SET TextString = REPLACE(TextString, 'µ', '&micro;') ;
END IF ;
#paragraph
IF INSTR( X , '¶' )
THEN SET TextString = REPLACE(TextString, '¶', '&para;') ;
END IF ;
#middle dot
IF INSTR( X , '·' )
THEN SET TextString = REPLACE(TextString, '·', '&middot;') ;
END IF ;
#spacing cedilla
IF INSTR( X , '¸' )
THEN SET TextString = REPLACE(TextString, '¸', '&cedil;') ;
END IF ;
#superscript 1
IF INSTR( X , '¹' )
THEN SET TextString = REPLACE(TextString, '¹', '&sup1;') ;
END IF ;
#masculine ordinal indicator
IF INSTR( X , 'º' )
THEN SET TextString = REPLACE(TextString, 'º', '&ordm;') ;
END IF ;
#angle quotation mark (right)
IF INSTR( X , '»' )
THEN SET TextString = REPLACE(TextString, '»', '&raquo;') ;
END IF ;
#fraction 1/4
IF INSTR( X , '¼' )
THEN SET TextString = REPLACE(TextString, '¼', '&frac14;') ;
END IF ;
#fraction 1/2
IF INSTR( X , '½' )
THEN SET TextString = REPLACE(TextString, '½', '&frac12;') ;
END IF ;
#fraction 3/4
IF INSTR( X , '¾' )
THEN SET TextString = REPLACE(TextString, '¾', '&frac34;') ;
END IF ;
#inverted question mark
IF INSTR( X , '¿' )
THEN SET TextString = REPLACE(TextString, '¿', '&iquest;') ;
END IF ;
#multiplication
IF INSTR( X , '×' )
THEN SET TextString = REPLACE(TextString, '×', '&times;') ;
END IF ;
#division
IF INSTR( X , '÷' )
THEN SET TextString = REPLACE(TextString, '÷', '&divide;') ;
END IF ;
#capital a, grave accent
IF INSTR( X , 'À' )
THEN SET TextString = REPLACE(TextString, 'À', '&Agrave;') ;
END IF ;
#capital a, acute accent
IF INSTR( X , 'Á' )
THEN SET TextString = REPLACE(TextString, 'Á', '&Aacute;') ;
END IF ;
#capital a, circumflex accent
IF INSTR( X , 'Â' )
THEN SET TextString = REPLACE(TextString, 'Â', '&Acirc;') ;
END IF ;
#capital a, tilde
IF INSTR( X , 'Ã' )
THEN SET TextString = REPLACE(TextString, 'Ã', '&Atilde;') ;
END IF ;
#capital a, umlaut mark
IF INSTR( X , 'Ä' )
THEN SET TextString = REPLACE(TextString, 'Ä', '&Auml;') ;
END IF ;
#capital a, ring
IF INSTR( X , 'Å' )
THEN SET TextString = REPLACE(TextString, 'Å', '&Aring;') ;
END IF ;
#capital ae
IF INSTR( X , 'Æ' )
THEN SET TextString = REPLACE(TextString, 'Æ', '&AElig;') ;
END IF ;
#capital c, cedilla
IF INSTR( X , 'Ç' )
THEN SET TextString = REPLACE(TextString, 'Ç', '&Ccedil;') ;
END IF ;
#capital e, grave accent
IF INSTR( X , 'È' )
THEN SET TextString = REPLACE(TextString, 'È', '&Egrave;') ;
END IF ;
#capital e, acute accent
IF INSTR( X , 'É' )
THEN SET TextString = REPLACE(TextString, 'É', '&Eacute;') ;
END IF ;
#capital e, circumflex accent
IF INSTR( X , 'Ê' )
THEN SET TextString = REPLACE(TextString, 'Ê', '&Ecirc;') ;
END IF ;
#capital e, umlaut mark
IF INSTR( X , 'Ë' )
THEN SET TextString = REPLACE(TextString, 'Ë', '&Euml;') ;
END IF ;
#capital i, grave accent
IF INSTR( X , 'Ì' )
THEN SET TextString = REPLACE(TextString, 'Ì', '&Igrave;') ;
END IF ;
#capital i, acute accent
IF INSTR( X , 'Í' )
THEN SET TextString = REPLACE(TextString, 'Í', '&Iacute;') ;
END IF ;
#capital i, circumflex accent
IF INSTR( X , 'Î' )
THEN SET TextString = REPLACE(TextString, 'Î', '&Icirc;') ;
END IF ;
#capital i, umlaut mark
IF INSTR( X , 'Ï' )
THEN SET TextString = REPLACE(TextString, 'Ï', '&Iuml;') ;
END IF ;
#capital eth, Icelandic
IF INSTR( X , 'Ð' )
THEN SET TextString = REPLACE(TextString, 'Ð', '&ETH;') ;
END IF ;
#capital n, tilde
IF INSTR( X , 'Ñ' )
THEN SET TextString = REPLACE(TextString, 'Ñ', '&Ntilde;') ;
END IF ;
#capital o, grave accent
IF INSTR( X , 'Ò' )
THEN SET TextString = REPLACE(TextString, 'Ò', '&Ograve;') ;
END IF ;
#capital o, acute accent
IF INSTR( X , 'Ó' )
THEN SET TextString = REPLACE(TextString, 'Ó', '&Oacute;') ;
END IF ;
#capital o, circumflex accent
IF INSTR( X , 'Ô' )
THEN SET TextString = REPLACE(TextString, 'Ô', '&Ocirc;') ;
END IF ;
#capital o, tilde
IF INSTR( X , 'Õ' )
THEN SET TextString = REPLACE(TextString, 'Õ', '&Otilde;') ;
END IF ;
#capital o, umlaut mark
IF INSTR( X , 'Ö' )
THEN SET TextString = REPLACE(TextString, 'Ö', '&Ouml;') ;
END IF ;
#capital o, slash
IF INSTR( X , 'Ø' )
THEN SET TextString = REPLACE(TextString, 'Ø', '&Oslash;') ;
END IF ;
#capital u, grave accent
IF INSTR( X , 'Ù' )
THEN SET TextString = REPLACE(TextString, 'Ù', '&Ugrave;') ;
END IF ;
#capital u, acute accent
IF INSTR( X , 'Ú' )
THEN SET TextString = REPLACE(TextString, 'Ú', '&Uacute;') ;
END IF ;
#capital u, circumflex accent
IF INSTR( X , 'Û' )
THEN SET TextString = REPLACE(TextString, 'Û', '&Ucirc;') ;
END IF ;
#capital u, umlaut mark
IF INSTR( X , 'Ü' )
THEN SET TextString = REPLACE(TextString, 'Ü', '&Uuml;') ;
END IF ;
#capital y, acute accent
IF INSTR( X , 'Ý' )
THEN SET TextString = REPLACE(TextString, 'Ý', '&Yacute;') ;
END IF ;
#capital THORN, Icelandic
IF INSTR( X , 'Þ' )
THEN SET TextString = REPLACE(TextString, 'Þ', '&THORN;') ;
END IF ;
#small sharp s, German
IF INSTR( X , 'ß' )
THEN SET TextString = REPLACE(TextString, 'ß', '&szlig;') ;
END IF ;
#small a, grave accent
IF INSTR( X , 'à' )
THEN SET TextString = REPLACE(TextString, 'à', '&agrave;') ;
END IF ;
#small a, acute accent
IF INSTR( X , 'á' )
THEN SET TextString = REPLACE(TextString, 'á', '&aacute;') ;
END IF ;
#small a, circumflex accent
IF INSTR( X , 'â' )
THEN SET TextString = REPLACE(TextString, 'â', '&acirc;') ;
END IF ;
#small a, tilde
IF INSTR( X , 'ã' )
THEN SET TextString = REPLACE(TextString, 'ã', '&atilde;') ;
END IF ;
#small a, umlaut mark
IF INSTR( X , 'ä' )
THEN SET TextString = REPLACE(TextString, 'ä', '&auml;') ;
END IF ;
#small a, ring
IF INSTR( X , 'å' )
THEN SET TextString = REPLACE(TextString, 'å', '&aring;') ;
END IF ;
#small ae
IF INSTR( X , 'æ' )
THEN SET TextString = REPLACE(TextString, 'æ', '&aelig;') ;
END IF ;
#small c, cedilla
IF INSTR( X , 'ç' )
THEN SET TextString = REPLACE(TextString, 'ç', '&ccedil;') ;
END IF ;
#small e, grave accent
IF INSTR( X , 'è' )
THEN SET TextString = REPLACE(TextString, 'è', '&egrave;') ;
END IF ;
#small e, acute accent
IF INSTR( X , 'é' )
THEN SET TextString = REPLACE(TextString, 'é', '&eacute;') ;
END IF ;
#small e, circumflex accent
IF INSTR( X , 'ê' )
THEN SET TextString = REPLACE(TextString, 'ê', '&ecirc;') ;
END IF ;
#small e, umlaut mark
IF INSTR( X , 'ë' )
THEN SET TextString = REPLACE(TextString, 'ë', '&euml;') ;
END IF ;
#small i, grave accent
IF INSTR( X , 'ì' )
THEN SET TextString = REPLACE(TextString, 'ì', '&igrave;') ;
END IF ;
#small i, acute accent
IF INSTR( X , 'í' )
THEN SET TextString = REPLACE(TextString, 'í', '&iacute;') ;
END IF ;
#small i, circumflex accent
IF INSTR( X , 'î' )
THEN SET TextString = REPLACE(TextString, 'î', '&icirc;') ;
END IF ;
#small i, umlaut mark
IF INSTR( X , 'ï' )
THEN SET TextString = REPLACE(TextString, 'ï', '&iuml;') ;
END IF ;
#small eth, Icelandic
IF INSTR( X , 'ð' )
THEN SET TextString = REPLACE(TextString, 'ð', '&eth;') ;
END IF ;
#small n, tilde
IF INSTR( X , 'ñ' )
THEN SET TextString = REPLACE(TextString, 'ñ', '&ntilde;') ;
END IF ;
#small o, grave accent
IF INSTR( X , 'ò' )
THEN SET TextString = REPLACE(TextString, 'ò', '&ograve;') ;
END IF ;
#small o, acute accent
IF INSTR( X , 'ó' )
THEN SET TextString = REPLACE(TextString, 'ó', '&oacute;') ;
END IF ;
#small o, circumflex accent
IF INSTR( X , 'ô' )
THEN SET TextString = REPLACE(TextString, 'ô', '&ocirc;') ;
END IF ;
#small o, tilde
IF INSTR( X , 'õ' )
THEN SET TextString = REPLACE(TextString, 'õ', '&otilde;') ;
END IF ;
#small o, umlaut mark
IF INSTR( X , 'ö' )
THEN SET TextString = REPLACE(TextString, 'ö', '&ouml;') ;
END IF ;
#small o, slash
IF INSTR( X , 'ø' )
THEN SET TextString = REPLACE(TextString, 'ø', '&oslash;') ;
END IF ;
#small u, grave accent
IF INSTR( X , 'ù' )
THEN SET TextString = REPLACE(TextString, 'ù', '&ugrave;') ;
END IF ;
#small u, acute accent
IF INSTR( X , 'ú' )
THEN SET TextString = REPLACE(TextString, 'ú', '&uacute;') ;
END IF ;
#small u, circumflex accent
IF INSTR( X , 'û' )
THEN SET TextString = REPLACE(TextString, 'û', '&ucirc;') ;
END IF ;
#small u, umlaut mark
IF INSTR( X , 'ü' )
THEN SET TextString = REPLACE(TextString, 'ü', '&uuml;') ;
END IF ;
#small y, acute accent
IF INSTR( X , 'ý' )
THEN SET TextString = REPLACE(TextString, 'ý', '&yacute;') ;
END IF ;
#small thorn, Icelandic
IF INSTR( X , 'þ' )
THEN SET TextString = REPLACE(TextString, 'þ', '&thorn;') ;
END IF ;
#small y, umlaut mark
IF INSTR( X , 'ÿ' )
THEN SET TextString = REPLACE(TextString, 'ÿ', '&yuml;') ;
END IF ;
RETURN TextString ;
END$$
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment