Skip to content

Instantly share code, notes, and snippets.

@zsmatrix62
Forked from ano/strip_tags.sql
Created May 14, 2024 06:45
Show Gist options
  • Save zsmatrix62/047a973d87ca234a97567b8555d3bd7b to your computer and use it in GitHub Desktop.
Save zsmatrix62/047a973d87ca234a97567b8555d3bd7b 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