Skip to content

Instantly share code, notes, and snippets.

@weird-coon
Last active June 14, 2018 08:11
Show Gist options
  • Save weird-coon/fad433e9dd696a5848e8443242427bcb to your computer and use it in GitHub Desktop.
Save weird-coon/fad433e9dd696a5848e8443242427bcb to your computer and use it in GitHub Desktop.
[MySQL] Reproduces PHP strip_tags () with Mysql and search keywords from the result of excluding HTML tags.
DELIMITER //
DROP FUNCTION IF EXISTS `STRIP_TAGS`//
CREATE FUNCTION STRIP_TAGS( x text) RETURNS text
LANGUAGE SQL NOT DETERMINISTIC READS SQL DATA
BEGIN
DECLARE sstart INT;
DECLARE ends INT;
IF x IS NOT NULL THEN
SET sstart = LOCATE('<', x, 1);
REPEAT
SET ends = LOCATE('>', x, sstart);
SET x = CONCAT(SUBSTRING( x, 1 ,sstart -1) ,SUBSTRING(x, ends +1 )) ;
SET sstart = LOCATE('<', x, 1);
UNTIL sstart < 1
END REPEAT;
END IF;
RETURN x;
END;
//
-- SELECT STRIP_TAGS('this <html>is <b>a test</b>, nothing more</html>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment