使用举例:
select func_url2domain('http://iamge.baidu.com/i?ct=503316480&z=0&tn=baiduimagedetail');
代码如下,也可下载附件啊
CREATE FUNCTION `func_url2domain`(url varchar(250)) RETURNS varchar(250) CHARSET utf8
READS SQL DATA
begin
DECLARE tmpdata varchar(250);
DECLARE tmpdata2 varchar(250);
set tmpdata=url;
-- delete http://
if Locate('http://',url)=1 then
set tmpdata=Substring(url,8);
end if;
-- delete https://
if Locate('https://',tmpdata)=1 then
set tmpdata=Substring(tmpdata,9);
end if;
-- delete www.
if Locate('www.',tmpdata)=1 then
set tmpdata=Substring(tmpdata,5);
end if;
-- delete str after /
if Locate('/',tmpdata)>0 then
set tmpdata=Substring(tmpdata,1,Locate('/',tmpdata)- 1);
end if;
-- delete second domain /
set tmpdata2=Substring(tmpdata,Locate('.',tmpdata)+1);
if Locate('com',tmpdata2)=1 or Locate('cn',tmpdata2)=1 or Locate('net',tmpdata2)=1 or Locate('org',tmpdata2)=1 or Locate('biz',tmpdata2)=1 or Locate('info',tmpdata2)=1 or Locate('cc',tmpdata2)=1 or Locate('tv',tmpdata2)=1 or Locate('in',tmpdata2)=1 or Locate('us',tmpdata2)=1 or Locate('hk',tmpdata2)=1 then
set tmpdata2=tmpdata;
else
set tmpdata=tmpdata2;
set tmpdata2=Substring(tmpdata,Locate('.',tmpdata)+1);
if Locate('com',tmpdata2)=1 or Locate('cn',tmpdata2)=1 or Locate('net',tmpdata2)=1 or Locate('org',tmpdata2)=1 or Locate('biz',tmpdata2)=1 or Locate('info',tmpdata2)=1 or Locate('cc',tmpdata2)=1 or Locate('tv',tmpdata2)=1 or Locate('in',tmpdata2)=1 or Locate('us',tmpdata2)=1 or Locate('hk',tmpdata2)=1 then
set tmpdata2=tmpdata;
else
set tmpdata=tmpdata2;
end if;
end if;
return lower(tmpdata);
end;