What is the MySQL query equivalent of PHP strip_tags?

后端 未结 9 697
情书的邮戳
情书的邮戳 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:16

    I'm using the lib_mysqludf_preg library for this and a regex like this:

    SELECT PREG_REPLACE('#<[^>]+>#',' ',cell) FROM table;
    

    Also did it like this for rows which with encoded html entities:

    SELECT PREG_REPLACE('#&lt;.+?&gt;#',' ',cell) FROM table;
    

    There are probably cases where these might fail but I haven't encountered any and they're reasonably fast.

    0 讨论(0)
  • 2020-11-27 19:18

    I am passing this code on, seems very similar to the above. Worked for me, hope it helps.

    BEGIN
      DECLARE iStart, iEnd, iLength   INT;
    
      WHILE locate('<', Dirty) > 0 AND locate('>', Dirty, locate('<', Dirty)) > 0
      DO
        BEGIN
          SET iStart = locate('<', Dirty), iEnd = locate('>', Dirty, locate('<', Dirty));
          SET iLength = (iEnd - iStart) + 1;
          IF iLength > 0 THEN
            BEGIN
              SET Dirty = insert(Dirty, iStart, iLength, '');
            END;
          END IF;
        END;
      END WHILE;
      RETURN Dirty;
    END
    
    0 讨论(0)
  • 2020-11-27 19:21

    REPLACE() works pretty well.

    The subtle approach:

     REPLACE(REPLACE(node.body,'<p>',''),'</p>','') as `post_content`
    

    ...and the not so subtle: (Converting strings into slugs)

     LOWER(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM(node.title), ':', ''), 'é', 'e'), ')', ''), '(', ''), ',', ''), '\\', ''), '\/', ''), '\"', ''), '?', ''), '\'', ''), '&', ''), '!', ''), '.', ''), '–', ''), ' ', '-'), '--', '-'), '--', '-'), '’', '')) as `post_name`
    
    0 讨论(0)
提交回复
热议问题