Skip to content

Instantly share code, notes, and snippets.

@ano
Last active May 14, 2024 06:45
Show Gist options
  • Save ano/a54d721234c29dce025a397b115fd3c0 to your computer and use it in GitHub Desktop.
Save ano/a54d721234c29dce025a397b115fd3c0 to your computer and use it in GitHub Desktop.
strip_tags MySQL user defined function to remove html tags
# Code to implement the strip_tags function in MySQL:
DELIMITER $$
CREATE FUNCTION strip_tags(html TEXT) RETURNS TEXT
BEGIN
DECLARE s TEXT;
DECLARE p INT DEFAULT 0;
IF html IS NULL THEN
RETURN NULL;
END IF;
SET s = html;
tags_loop: WHILE INSTR(s, '<') <> 0 AND p < LENGTH(s) DO
SET p = LOCATE('<', s);
IF p > 0 THEN
SET s = CONCAT(SUBSTRING(s, 1, p - 1), SUBSTRING(s, LOCATE('>', s, p) + 1));
END IF;
END WHILE;
RETURN TRIM(s);
END$$
DELIMITER ;
To use this function, simply call it in your MySQL query like this:
SELECT strip_tags('<b>Hello</b> World!');
The output will be "Hello World!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment