在MariaDB中从html中删除样式标签

dldeef67  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(101)

我已经在MariaDB数据库中的HTML长文本,并希望删除其内容的样式标签。我目前正在尝试以下内容:

SET GLOBAL log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS fnStripStyleRXTag;
DELIMITER |
CREATE FUNCTION fnStripStyleRXTag( Dirty longtext )
RETURNS longtext
DETERMINISTIC 
BEGIN
  DECLARE iStart, iEnd, iLength int;
    WHILE Locate( '<style', Dirty ) > 0 And Locate( '</style>', Dirty, Locate( '<style', Dirty )) > 0 DO
      BEGIN
        SET iStart = Locate( '<style', Dirty ), iEnd = Locate( '</style>', Dirty, Locate('<style', Dirty ));
        SET iLength = ( iEnd - iStart) + 8;
        IF iLength > 0 THEN
          BEGIN
            SET Dirty = Insert( Dirty, iStart, iLength, '');
          END;
        END IF;
      END;
    END WHILE;
    RETURN Dirty;
END;
|
DELIMITER ;
SELECT fnStripStyleRXTag('<style id="test">nothing more</style>this <html>is <b>a test</b>, <style class="testovic">nothing more</style></html>');

这给了我以下结果:

+--------------------------------------------------------------------------------------------------------------------------------------------+
| fnStripStyleRXTag('<style id="test">nothing more</style>this <html>is <b>a test</b>, <style class="testovic">nothing more</style></html><style>hat</style>') |
+--------------------------------------------------------------------------------------------------------------------------------------------+
| this <html>is <b>a test</b>, </html>                                                                                                       |
+--------------------------------------------------------------------------------------------------------------------------------------------+

所以基本上它完成了这项工作,但有一些错误的空间。所以我想知道是否有一个更聪明的方法来做这件事。我想用一个Locate( '<style%', Dirty ),但这不起作用。
真的很感谢任何线索和帮助。谢谢你,谢谢!

更新:

SELECT VERSION();
+---------------------+
| VERSION()           |
+---------------------+
| 10.11.5-MariaDB-log |
+---------------------+
kknvjkwl

kknvjkwl1#

我不确定这是否更好,但你可以使用REGEXP_REPLACE()来实现同样的结果:

SET @string = '<style id="test">nothing more</style>this <html>is <b>a test</b>, <style class="testovic">nothing more</style></html>';
SELECT REGEXP_REPLACE(@string, '<style.*?</style>', '');

输出量:

+--------------------------------------------------+
| REGEXP_REPLACE(@string, '<style.*?</style>', '') |
+--------------------------------------------------+
| this <html>is <b>a test</b>, </html>             |
+--------------------------------------------------+

相关问题