当前位置:数据库 > SQLServer >>

三段过滤字段html的mssql函数代码转发

sql 过滤html 标签  

--方法
create function [dbo].[clearhtml_a] (@maco varchar(8000)) 
returns varchar(8000) as begin 
    declare @i int 
    while 1 = 1 
    begin 
       set @i=len(@maco) 
       set @maco=replace(@maco, substring(@maco,charindex('<a',@maco), 
       charindex('a>',@maco)-charindex('<a',@maco)+1),space(0)) 
       if @i=len( @maco ) 
       break 
    end 
  
    set @maco=replace(@maco,' ','') 
    set @maco=replace(@maco,' ','') 
    set @maco=ltrim(rtrim(@maco)) 
    set @maco=replace(@maco,char(9),'') 
    set @maco=replace(@maco,char(10),'') 
    set @maco=replace(@maco,char(13),'') 
  
    return (@maco) 
end 

--测试
declare @mark varchar(8000) 
set @mark='输入代码html标志的内容' 
select dbo.clearhtml (@mark) 

--加强版
create function [dbo].[clearhtml_V2] (@maco varchar(8000)) 
returns varchar(8000) 
as 
begin 
    declare @randchar_one nvarchar(200) 
    declare @randchar_two nvarchar(200) 
       if(charindex('<<',@maco)>0) 
              begin 
                     set @randchar_one='D4678B36-B958-4274-B81E-BBA636CFB427'; 
                     set @randchar_two='49E374CC-9E1A-4850-897C-27074DE32E7F'; 
                     set @maco=replace(@maco,'<<',@randchar_one) 
                     set @maco=replace(@maco,'>>',@randchar_two) 
              end 
    declare @i int 
    while 1 = 1 
    begin 
       set @i=len(@maco) 
       set @maco=replace(@maco, substring(@maco,charindex('<',@maco), 
       charindex('>',@maco)-charindex('<',@maco)+1),space(0)) 
       if @i=len( @maco ) 
       break 
    end 
  
    set @maco=replace(@maco,' ','') 
    set @maco=replace(@maco,' ','') 
    set @maco=ltrim(rtrim(@maco)) 
    set @maco=replace(@maco,char(9),'') 
    set @maco=replace(@maco,char(10),'') 
    set @maco=replace(@maco,char(13),'') 
    if(charindex(@randchar_one,@maco)>0) 
    begin 
       set @maco=replace(@maco,'D4678B36-B958-4274-B81E-BBA636CFB427','<<') 
       set @maco=replace(@maco,'49E374CC-9E1A-4850-897C-27074DE32E7F','>>') 
    end 
    return (@maco) 
end 
--测试 
select dbo.clearhtml_V2('输入代码html标志的内容然后过滤') 

--完整版
ALTER FUNCTION [dbo].[CleanHTML] (@HTMLText VARCHAR(MAX)) 
RETURNS VARCHAR(MAX) AS 
BEGIN 
    DECLARE @Start INT 
    DECLARE @End INT 
    DECLARE @Length INT 
    SET @Start = CHARINDEX('<',@HTMLText) 
    SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText)) 
    SET @Length = (@End - @Start) + 1 
    WHILE @Start > 0 AND @End > 0 AND @Length > 0 
    BEGIN 
        SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'') 
        SET @Start = CHARINDEX('<',@HTMLText) 
        SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText)) 
        SET @Length = (@End - @Start) + 1 
    END 
    set @HTMLText=replace(@HTMLText,'<','')
    set @HTMLText=replace(@HTMLText,'>','')
    RETURN LTRIM(RTRIM(@HTMLText)) 
END 

select [dbo].DeHtmlize('输入代码html标志的内容然后过滤')


Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,