如果要過濾html標(biāo)簽多半同學(xué)都使用php的函數(shù)了,但是大家不知道是可以直接在mysql中進(jìn)行去除htm標(biāo)簽吧,下面一起來看看吧.
mysql本身沒有去除html代碼的內(nèi)置函數(shù),但是在一些情況下,不得不在數(shù)據(jù)庫層次提取一些去除了html代碼的純文本.
經(jīng)過谷歌后,找到了以下兩個函數(shù),經(jīng)測試,均可用.
函數(shù)1,代碼如下:
- 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 --Vevb.com
- SET Dirty = Insert( Dirty, iStart, iLength, '');
- END;
- END IF;
- END;
- END WHILE;
- RETURN Dirty;
- END;
- |
- DELIMITER ;
- SELECT fnStripTags('this <html>is <b>a test</b>, nothing more</html>');
函數(shù)2,代碼如下:
- 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;
- select strip_tags('<span>hel<b>lo <a href="world">wo<>rld</a> <<x>again<.');
新聞熱點
疑難解答
圖片精選