Skip to content

Instantly share code, notes, and snippets.

@kovyrin
Created October 29, 2008 03:42
Show Gist options
  • Save kovyrin/20615 to your computer and use it in GitHub Desktop.
Save kovyrin/20615 to your computer and use it in GitHub Desktop.
mysql> CREATE FUNCTION `xml_escape_with_br`( tagvalue VARCHAR(2000)) RETURNS varchar(2000) DETERMINISTIC BEGIN IF (tagvalue IS NULL) THEN RETURN null; END IF; RETURN REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE( tagvalue,'&','&amp;'), '<','&lt;'), '>','&gt;'), '"','&quot;'), '\'','&apos;'), '\n', '<br/>'); END$$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> select xml_escape_with_br("<h1>hello, world!</h1>\n<p>We say: 'Alexey & Tikhon rock!'");
+-----------------------------------------------------------------------------------------------+
| xml_escape_with_br("<h1>hello, world!</h1>\n<p>We say: 'Alexey & Tikhon rock!'") |
+-----------------------------------------------------------------------------------------------+
| &lt;h1&gt;hello, world!&lt;/h1&gt;<br/>&lt;p&gt;We say: &apos;Alexey &amp; Tikhon rock!&apos; |
+-----------------------------------------------------------------------------------------------+
1 row in set (0.04 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment