What is the MySQL query equivalent of PHP strip_tags?

后端 未结 9 779
情书的邮戳
情书的邮戳 2020-11-27 18:49

I have a large database which contains records that have tags in them and I would like to remove them. Of course there is the method where I create a

9条回答
  •  失恋的感觉
    2020-11-27 19:12

    Here you go:

    CREATE FUNCTION `strip_tags`($str text) RETURNS text
    BEGIN
        DECLARE $start, $end INT DEFAULT 1;
        LOOP
            SET $start = LOCATE("<", $str, $start);
            IF (!$start) THEN RETURN $str; END IF;
            SET $end = LOCATE(">", $str, $start);
            IF (!$end) THEN SET $end = $start; END IF;
            SET $str = INSERT($str, $start, $end - $start + 1, "");
        END LOOP;
    END;
    

    I made sure it removes mismatched opening brackets because they're dangerous, though it ignores any unpaired closing brackets because they're harmless.

    mysql> select strip_tags('hello wo<>rld <again<.');
    +----------------------------------------------------------------------+
    | strip_tags('hello wo<>rld <again<.') |
    +----------------------------------------------------------------------+
    | hello world again.                                                   |
    +----------------------------------------------------------------------+
    1 row in set
    

提交回复
热议问题