实现 PHP strip_tags 函数的功能。
CREATE DEFINER=`dba`@`%` FUNCTION `strip_tags`(`$str` TEXT) RETURNS text CHARSET utf8 LANGUAGE SQL NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' 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
mysql> select strip_tags('<span><i>hello</i> <b>world</b>!!! <br /><a href="//www.netkiller/">netkiller</a>'); +----------------------------------------------------------------------+ | strip_tags('<span>hel<b>lo <a href="world">wo<>rld</a> <<x>again<.') | +----------------------------------------------------------------------+ | hello world again. | +----------------------------------------------------------------------+ 1 row in set mysql> select strip_tags('<span style="color:red"><i>hello</i> <b id="world" >world</b>!!! <br /><a class="home" href="//www.netkiller/">netkiller</a><span>') as TEXT; +--------------------------+ | TEXT | +--------------------------+ | hello world!!! netkiller | +--------------------------+ 1 row in set (0.00 sec)
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。