[数据库]为MySQL 增加 HTML 反转义函数

create function htmlDecode (s varchar(65536))
returns varchar(65536)
begin
declare charIndex int;
declare charEnd int;
declare charCode int;
declare retValue varchar(65536);
declare htmlChar varchar(8);
declare Len int;
set charIndex=1;
set Len=char_length(s);
set retValue='';
while charIndex<=Len do
	if substring(s,charIndex,2)='&#' then
		begin
		set charIndex=charIndex+2;
		set charEnd=charIndex;
		labelDecode:while charEnd<Len do
			if substring(s,charEnd,1)=';' then
				leave labelDecode;
			else
				set charEnd=charEnd+1;
			end if;
		end while;
		set htmlChar=substring(s,charIndex,charEnd-charIndex);
		set charCode=cast(htmlChar as signed);
		set retValue=concat(retValue,char(charCode using ucs2));
		set charIndex=charEnd+1;
		end;
	else
		begin
		set retValue=concat(retValue,substring(s,charIndex,1));
		set charIndex=charIndex+1;
		end;
	end if;	
end while;
return retValue;
end;
分享到: