知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
實現 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)