创建函数strip_tags

CREATE FUNCTION `strip_tags`($str text) RETURNS text BEGIN DECLARE $start, $end INT DEFAULT 1; LOOP SET $start = LOCATE("

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;

查看函数strip_tags

mysql> show function status \G

*************************** 1. row ***************************

Db: sijitao.net

Name: strip_tags

Type: FUNCTION

Definer: root@localhost

Modified: 2014-12-10 09:40:00

Created: 2014-12-10 09:40:00

Security_type: DEFINER

Comment:

character_set_client: utf8

collation_connection: utf8_general_ci

Database Collation: utf8_general_ci

1 row in set (0.00 sec)

mysql> show create function strip_tags \G

*************************** 1. row ***************************

Function: strip_tags

sql_mode:

Create Function: CREATE DEFINER=`root`@`localhost` FUNCTION `strip_tags`($str text) RETURNS text CHARSET utf8

BEGIN

DECLARE $start, $end INT DEFAULT 1;

SET $start = LOCATE("

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;

character_set_client: utf8

collation_connection: utf8_general_ci

Database Collation: utf8_general_ci

1 row in set (0.00 sec)

测试函数strip_tags

mysql> select strip_tags('hel lo wo<>rld <again<. strip_tags>hel lo wo<>rld <again<.>

+----------------------------------------------------------------------+

| hello world again. |

+----------------------------------------------------------------------+

1 row in set (0.00 sec)

测试结果满足要求,已经自动帮我们删除html标签。

遇到的问题

我在创建函数的时候碰到了如下类似错误。

ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)

解决办法是在创建函数之前执行下面这个命令。

mysql> set global log_bin_trust_function_creators=1;

通过除去绑定程序和符号调试程序使用的信息,减少扩展公共对象文件格式(XCOFF)的对象文件的大小。语法描述 strip 命令减少 XCOFF 对象文件的大小。 strip 命令从 XCOFF 对象文件 有选择地除去行号信息、重定位信息、调试段、typchk 段、注释段、文件头以及所有或部分符号表。 一旦您使用该命令,则很难调试文件的符号;因此,通常应该只在已经调试和测试过的生成模块上使用 strip 命... DROP FUNCTION IF EXISTS fn Strip Tags ; DELIMITER | CREATE FUNCTION fn Strip Tags ( Dirty text CHARSET utf8) RETURNS text CHARSET utf8 DETERMINISTIC BEGIN DECLARE iStart, iEnd, iLength int; WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Di CREATE FUNCTION ` strip _ tags `($str text) RETURNS text BEGIN DECLARE $start, $end INT DEFAULT 1; SET $start = LOC... 如果要过滤 html 标签 多半同学都使用php的 函数 了,但是大家不知道是可以直接在 mysql 进行去除htm 标签 吧,下面一起来看看吧。 mysql 本身没有去除 html 代码的内置 函数 ,但是在一些情况下,不得不在数据库层次提取一些去除了 html 代码的纯文本。 经过谷歌后,找到了以下 函数 ,经测试,可用。第二个phpmyadmin 报错。navicat 可以以 SET GLOBAL lo 创建 函数 strip _ tags CREATE FUNCTION ` strip _ tags `($str text) RETURNS textBEGINDECLARE $start, $end INT DEFAULT 1;LOOPSET $start = LOCATE(", $str, $start);IF (!$start) THEN RETURN $str; END IF;SET $end = LOCA... 本文实例讲述了php字符串过滤 strip _ tags () 函数 用法。分享给大家供大家参考,具体如下: strip _ tags — 从字符串 去除 HTML 和 PHP 标记,非常重要的 函数 (PHP 4, PHP 5, PHP 7)string strip _ tags ( string $str [, string $allowable_ tags ] )$str:输入字符串。$allowable_ tags :可...