« mysql backup shell script | Home | How MySQL Optimises ORDER BY »

March 4, 2008

Removing HTML tags using mysql

Try this (ported from a T-SQL func by Robert Davis):

SET GLOBAL log_bin_trust_function_creators=1;
DROP FUNCTION IF EXISTS fnStripTags;
DELIMITER |
CREATE FUNCTION fnStripTags( Dirty varchar(4000) )
RETURNS varchar(4000)
DETERMINISTIC
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;
|
DELIMITER ;
SELECT fnStripTags(`this is a test, nothing more`);

Posted by Lifeng Shen on March 4, 2008 9:23 AM |

评论

添加评论







固定链接与引用